473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Left shift operator (<<), why?

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,
}
Nov 16 '05 #1
4 13030
In article <uH*************@TK2MSFTNGP10.phx.gbl>, kc@noneya.com says...
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.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 16 '05 #2

"Kevin" <kc@noneya.com> wrote in message
news:uH*************@TK2MSFTNGP10.phx.gbl...
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
Nov 16 '05 #3
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,
}

-----Original Message-----
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 anleft-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,
}
.

Nov 16 '05 #4
Cool, got it ... makes sense. Thanks!

"Mythran" <ki********@hotmail.com> wrote in message
news:O$**************@TK2MSFTNGP11.phx.gbl...

"Kevin" <kc@noneya.com> wrote in message
news:uH*************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
2
by: William White | last post by:
Just thought of this, in reference to earlier discussion about replacing &< and &> with !<< and !>> to correct the problem w/ rtree search strategy "replacement" at the node level. It's possibly...
19
by: aurgathor | last post by:
I use BC 5.02 (long is 32 bit) and I wonder if there's any efficient code that would allow me to left shift 64 bit values that I currently use this way: typedef struct { unsigned long lsLong;...
4
by: sandhya | last post by:
Hello Folks, i hava a problem in coding of circular left shift of 25 bits in my program...how do i perform it, and how do i use unsigned in VB. My program (IDEA algorithm implementation in VB) ...
56
by: Christian Christmann | last post by:
Hi, in the header of my class I've a constant static const int a = ( 1 << 32 ) - 1; When compiling the code, g++ issues the warning "warning: left shift count >= width of type" Why? And...
24
by: Nishu | last post by:
Hi All, Could you please explain whether C standard supports logical right shift operation using some operator? I know somewhere I read about >>operator. I thought, it is in C but i think i'm...
16
by: Santosh Nayak | last post by:
Hi, Is there any way to catch the losing bit occurring due to Right Shift Operator ? e.g int a = 5 ; a = a >1 ; // // a is now 2 and the least significant bit is lost // // I want this...
2
by: Peter Lee | last post by:
ASSUME unsigned int = 32 bits #q1 unsigned int u1 = 3u << 0u valid? #q2 unsigned int u2 = 3u >0u valid? #q3 unsigned int u3 = 3u << 32u valid? #q4 unsigned int u4 = 3u >32u valid?
4
by: G Iveco | last post by:
I am using this type of code to do right-shifting, B = 3; data1 = (data + (1 << (B-1))) >B; data1 seems incorrect when data = -4-8*i.. which means it rounds -1.5 to -1 instead of -2. On...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.