We use bitwise and “&” to test if certain bit(s) are one or not. And’ing a value against a pattern with ones only in the bit positions you are interested in will give zero if none of them are on, nonzero if one or more is on. We can also use bitwise and “&” to turn off (set to zero) any desired bit(s). If you and”&” a pattern against a variable, bit positions in the pattern that are ones will leave the target bit unchanged, and bit positions in the pattern that are zeros will set the target bit to zero. See an example below:
1 & 0 = 0
0 & 0 = 0
0 & 1 = 0
1 & 1 = 1
Bitwise or”|” is used to turn on (set to one) desired bit(s). Or’ing a value against a set or un set bit gives a set bit. See an example:
1 | 0 = 1
0 | 0 = 0
0 | 1 = 1
1 | 1 = 1
Bitwise exclusive or “^” is used to flip or reverse the setting of desired bit(s) (make it a one if it was zero or make it zero if it was one). See an example:
1 ^ 0 = 1
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 1 = 0
Operators >> (right shift) and << (left shift) can be used to shift the bits of an operand to the right or left a desired number of positions. The number of positions to be shifted can be specified as a constant, in a variable or as an expression. Bits shifted out are lost. For left shifts, bit positions vacated by shifting always filled with zeros. For right shifts, bit positions vacated by shifting filled with zeros for unsigned data type and with copy of the highest (sign) bit for signed data type. The right shift operator can be used to achieve quick multiplication by a power of 2. Similarly the right shift operator can be used to do a quick division by power of 2 (unsigned types only). The operators >> and <<, don’t change the operand at all. However, the operators >>= and <=< also change the operand after doing the shift operations. x << y – Gives value x shifted left y bits (Bits positions vacated by shift are filled with zeros). x <<= y – Shifts variable x left y bits (Bits positions vacated by shift are filled with zeros). A left shift of n bits multiplies by 2 raise to n. x >> y – Gives value x shifted right y bits. x >>= y – Shifts variable x right y bits. For the right shift, all bits of operand participate in the shift. For unsigned data type, bits positions vacated by shift are filled with zeros. For signed data type, bits positions vacated by shift are filled with the original highest bit (sign bit). Right shifting n bits divides by 2 raise to n. Shifting signed values may fail because for negative values the result never
x << 2 – Gives value x shifted left 2 bits (Bits positions vacated by shift are filled with zeros).
A left shift of n bits multiplies by 2 raise to n.
x >> 2 – Gives value x shifted right 2 bits.
For the right shift, All bits of operand participate in the shift. For unsigned data type, bits positions vacated by shift are filled with zeros. For signed data type, bits positions vacated by shift are filled with the original highest bit (sign bit). Right shifting n bits divides by 2 raise to n. Shifting signed values may fail because for negative values the result never gets past -1: -5 >> 3 is -1 and not 0 like -5/8.