What does this ">> =" operator mean in C?
The expression set >>= 1; means set = set >> 1; , i.e. right shift bits set by 1 (self-tuning form >> bitwise shift shift operator Bitwise shift operators ).
Assume set :
BIT NUMBER 31 n=27 m=17 0 โผ โผ โผ โผ set = 0000 1111 1111 1110 0000 0000 0000 0000 Then after set >> = 1; the set variable becomes:
BIT NUMBER 31 n=26 m=16 0 โผ โผ โผ โผ set = 0000 0111 1111 1111 0000 0000 0000 0000 Note that the number of bits is shifted.
Pay attention to an interesting point: since set is unsigned long , this operation >> must be a logical shift ( unsigned shift ), a logical shift does not save the bit of the sign of a number.
Also, since you are shifting all the bits to the right (towards a lower significant number), therefore, one right shift = division by two.
check this code (just to demonstrate the last point):
int main(){ unsigned long set = 268304384UL; set >>= 1; printf(" set :%lu \n", set); set = 268304384UL; set /= 2; printf(" set :%lu \n", set); return 1; } And the conclusion:
set :134152192 set :134152192 (note: it does not mean >> and / both)
Similarly, you have the <<= operator for left shift, check the other available Bitwise operators and Compound assignment operators , also check the section: bit expressions and the difference between: signed / arithmetic shift and unsigned shift .
This โright shiftโ means a value of one bit. If you move all the bits of an integer to the right by 1, you actually "divide by 2" because the binary code is a numbering system for base 2.
Imagine you have number 12 in binary format:
1100 = 12 in binary 110 = 6 in binary (1100 right-shifted) Just as if you moved all the numbers in base-10 to the right by one, you would divide by 10.
This shifts the bit to the right by 1, which is equivalent to dividing by 2. For more information on bit offsets, see http://msdn.microsoft.com/en-us/library/f96c63ed(v=vs.80).aspx
Each binary operator can be combined with = . In all cases
dest op= expression equivalently
dest = dest op expression (except when dest has any side effects, they occur only once).
So that means that
set>>=1; is equivalent to:
set = set >> 1; Since >> is a binary operator with a right shift, this means shifting the value in set right by 1 bit.
The above command performs a right shift of one bit. Repeat bit wise operations in c from this link http://www.cprogramming.com/tutorial/bitwise_operators.html