Bitwise Operators
For example, consider the number 3, which is binary 0011:
3 = 0011
3 << 1 = 0110 = 6
3 << 2 = 1100 = 12
3 << 3 = 1000 = 8
Note that in the third case, we shifted a bit off the end of the number! Bits that are shifted off the end of the binary number are lost forever.
(Reminder: We're working with 4-bit values here. With an 8-bit value, 3 << 3
would be 0001 1000
, which is decimal 24
. In this case, the 4th bit wouldn't be shifted off the left end of the binary number).
The bitwise right shift (>>) operator shifts bits to the right.
12 = 1100
12 >> 1 = 0110 = 6
12 >> 2 = 0011 = 3
12 >> 3 = 0001 = 1
Note that in the third case we shifted a bit off the right end of the number, so it is lost.
Interesting Facts about Bitwise Operators in C
http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/
https://www.geeksforgeeks.org/interesting-facts-bitwise-operators-c/