473,569 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,
InterestingPare nts = Interesting | Parents,
InterestingChil dren = Interesting | Children,
ParentsChildren = Parents | Children,
}
Nov 16 '05 #1
4 13047
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,
InterestingPare nts = Interesting | Parents,
InterestingChil dren = 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.co m> wrote in message
news:uH******** *****@TK2MSFTNG P10.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,
InterestingPare nts = Interesting | Parents,
InterestingChil dren = 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.Bo olean1 | BooleanFlags.Bo olean3;

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,
InterestingPare nts = 0110,
InterestingChil dren = 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,
InterestingPare nts = Interesting | Parents,
InterestingChil dren = Interesting | Children,
ParentsChildren = Parents | Children,
}
.

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

"Mythran" <ki********@hot mail.com> wrote in message
news:O$******** ******@TK2MSFTN GP11.phx.gbl...

"Kevin" <kc@noneya.co m> wrote in message
news:uH******** *****@TK2MSFTNG P10.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,
InterestingPare nts = Interesting | Parents,
InterestingChil dren = 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.Bo olean1 | BooleanFlags.Bo olean3;

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
26459
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
1259
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 nit-picking but I'm that way. Again, using 1D definitions here. S << T can be (and I would expect, usually is) defined as "for all x in S and...
19
2518
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; unsigned long msLong; } b64_struct;
4
4262
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) requires unsigned bits...so how do i go thro this ,since VB does not support unsigned operations Post your suggestions!!!
56
15306
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 how can I get rid of that?
24
3156
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 wrong about it. No where google helps me determining this lapse in my memory. MSVC 6 compiler gives me error. Thanks && Regards,
16
3712
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 solution for the question:
2
2402
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
4201
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 the positive side, 1.5 is rounded to 2, which ic correct.
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8132
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6286
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.