Blog Archive

Friday, June 1, 2018

[bit operator] Using the << and >> Operators on Bits - C++ Forum

Using the << and >> Operators on Bits - C++ Forum: "

<< left shifts, and >> right shifts.


1
2
3
4
5
6
7
int foo = 0xF0;

foo >>= 4;
// foo == 0x0F

foo <<= 2;
// foo == 0x3C 



EDIT: for those unaware of what bitshifting is...

0xF0 is %11110000 in binary:


1
2
3
4
5
6
 hex      bin 
0xF0   %11110000   <-- original
0x78   %01111000   <-- right shift 1
0x3C   111100   <-- right shift 2
0x1E   011110   <-- right shift 3
0x3C   001111   <-- right shift 4


Right shifting by X is similar (but not exactly the same as) dividing by 2X, and left shifting by X is similar (but not exactly the same as multiply by 2X

'via Blog this'

No comments:

Post a Comment