Help please

adam_b19's Avatar, Join Date: Mar 2007
Newbie Member
Hi can someone please help me,

I have got this code below and I do not know what the "<<" and the ">>" operators functionalities are. The code is basically;

unsigned long nn, m, n;

nn = 512;

n=nn << 1;         //these are the 3 lines of code that I do not understand
m=n >> 1;
m >>= 1;
Any help would be appreciated


Last edited by shabbir; 30Mar2007 at 07:19.. Reason: Code formatting
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Please read about the proper way to post code. The operators you show are binary shift operators. In the first line the binary value, 512, is being shifted left one bit. This results in a value of 1024. In the second line, the value of n (now 1024) is being shifted right one bit. This results in the original value, 512. The third line is merely shorthand for "m = m >> 1;", which results in the value 256.

In a left shift, the rightmost bit becomes a 0. In a right shift, the leftmost bit becomes a zero for unsigned variables, or a duplicate of its previous (before the shift) value for signed variables.
wrecker's Avatar, Join Date: Mar 2007
Go4Expert Member
These are Bit wise shifting operators that shift bits of the variable provided on left hand side of << / >> , number of times provided on right hand side of operators. by discarding the carry bits......
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Thanks for clearing that up.