Left shift operator (<<), why?

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

I was looking through some source code and noticed the used of the C# <<
operator. Why is this being used here and under what circumstances is an
left-shift operator useful.

internal enum InterestLevel
{
Ignore = 0,
Display = 1<<0,
Interesting = 1<<1,
Parents = 1<<2,
Children = 1<<3,
InterestingParents = Interesting | Parents,
InterestingChildren = Interesting | Children,
ParentsChildren = Parents | Children,
}
 
internal enum InterestLevel
{
Ignore = 0,
Display = 1<<0,
Interesting = 1<<1,
Parents = 1<<2,
Children = 1<<3,
InterestingParents = Interesting | Parents,
InterestingChildren = Interesting | Children,
ParentsChildren = Parents | Children,
}

Someone's personal preference. It makes no difference to .NET. You can
use "1<<0", "1<<1", "1<<2" or use "1", "2", "4" -- the same values get
compiled into the IL.
 
Kevin said:
I was looking through some source code and noticed the used of the C# <<
operator. Why is this being used here and under what circumstances is an
left-shift operator useful.

internal enum InterestLevel
{
Ignore = 0,
Display = 1<<0,
Interesting = 1<<1,
Parents = 1<<2,
Children = 1<<3,
InterestingParents = Interesting | Parents,
InterestingChildren = Interesting | Children,
ParentsChildren = Parents | Children,
}

Simply? Bit shifting allows for compact storage of similar data as a single
integral value. For example, say you have three True of False values. You could
have 3 separate variables called Boolean1, Boolean2, and Boolean3. Now, you save
these to a file, database, whatever. You'll have 3 separate values to store
which takes up that much memory.

Now, if you switch to bit vars, you can do the same thing as listed below:

internal enum BooleanFlags {
Boolean1 = 1 << 0,
Boolean2 = 1 << 1,
Boolean3 = 1 << 2
}

To use these values, you'll have to do bitwise comparison and shifting to get the
values, but you only require enough space on disk / memory for a single integer
value. Example:

BooleanFlags MyValue = BooleanFlags.Boolean1 | BooleanFlags.Boolean3;

Now, Boolean1 = True and Boolean3 = True and is stored shifted in the value
MyValue as a single integer.

Get it?

Bah, guess it's a little hard for me to explain. I learned about these while
learning C/C++ from CircleMud (www.circlemud.org) and continue to use them. They
were mostly used when memory (both hard disk space as well as RAM) were sparse.

Hope I helped at least a little.

Mythran
 
The left shift operator, '<<', is used to shift the bits
of a variable to the left. Since ALL data is stored as a
string of (binary) bits, a single shift multiplies or
divides by two. On Intel (and clones) machines they use
the representation called Little Endian, that is the least
significant bit on the end. Thus, the left shift
multiplies by two on Intel machines.

For the example below the left shift is being used to
create what my colleagues and I call Flags. A single Flag
is being set for four items. A combination of Flags are
being set for the last three items.

Also, please note that a single pipe, '|', is a bitwise
OR. Don't confuse it with the double pipe, '||', that is
a logical OR.

The enum becomes (in binary):

internal enum InterestLevel
{
Ignore = 0000,
Display = 0001,
Interesting = 0010,
Parents = 0100,
Children = 1000,
InterestingParents = 0110,
InterestingChildren = 1010,
ParentsChildren = 1100,
}
 
Cool, got it ... makes sense. Thanks!

Mythran said:
Simply? Bit shifting allows for compact storage of similar data as a single
integral value. For example, say you have three True of False values. You could
have 3 separate variables called Boolean1, Boolean2, and Boolean3. Now, you save
these to a file, database, whatever. You'll have 3 separate values to store
which takes up that much memory.

Now, if you switch to bit vars, you can do the same thing as listed below:

internal enum BooleanFlags {
Boolean1 = 1 << 0,
Boolean2 = 1 << 1,
Boolean3 = 1 << 2
}

To use these values, you'll have to do bitwise comparison and shifting to get the
values, but you only require enough space on disk / memory for a single integer
value. Example:

BooleanFlags MyValue = BooleanFlags.Boolean1 | BooleanFlags.Boolean3;

Now, Boolean1 = True and Boolean3 = True and is stored shifted in the value
MyValue as a single integer.

Get it?

Bah, guess it's a little hard for me to explain. I learned about these while
learning C/C++ from CircleMud (www.circlemud.org) and continue to use them. They
were mostly used when memory (both hard disk space as well as RAM) were sparse.

Hope I helped at least a little.

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top