Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help please (http://www.go4expert.com/forums/help-please-t3676/)

adam_b19 30Mar2007 02:54

Help please
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


DaWei 30Mar2007 03:34

Re: Help please
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 3Apr2007 15:11

Re: Help please
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 3Apr2007 17:42

Re: Help please
Thanks for clearing that up.

All times are GMT +5.5. The time now is 00:33.