Bitwise Operators
Use the bitwise operators to modify individual bits of numerical operands.
Bitwise operators associate from left to right. The only exception is the bitwise complement operator ~
which associates from right to left.
Bitwise Operators Overview
Operator | Operation | Precedence |
---|---|---|
& | bitwise AND; compares pairs of bits and returns 1 if both bits are 1, otherwise returns 0 | 8 |
| | bitwise (inclusive) OR; compares pairs of bits and returns 1 if either or both bits are 1, otherwise returns 0 | 6 |
^ | bitwise exclusive OR (XOR); compares pairs of bits and returns 1 if the bits are complementary, otherwise returns 0 | 7 |
~ | bitwise complement (unary); inverts each bit | 14 |
<< | bitwise shift left; moves the bits to the left, discards the far left bit and assigns 0 to the far right bit. | 11 |
>> | bitwise shift right; moves the bits to the right, discards the far right bit and if unsigned assigns 0 to the far left bit, otherwise sign extends | 11 |
Logical Operations on Bit Level
& | 0 | 1 |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
| | 0 | 1 |
---|---|---|
0 | 0 | 1 |
1 | 1 | 1 |
^ | 0 | 1 |
---|---|---|
0 | 0 | 1 |
1 | 1 | 0 |
~ | 0 | 1 |
---|---|---|
1 | 0 |
Bitwise operators &
, |
and ^
perform logical operations on the appropriate pairs of bits of their operands. Operator ~
complements each bit of its operand. For example:
0x1234 & 0x5678 /* equals 0x1230 */ /* because .. 0x1234 : 0001 0010 0011 0100 0x5678 : 0101 0110 0111 1000 ---------------------------- & : 0001 0010 0011 0000 .. that is, 0x1230 */
/* Similarly: */ 0x1234 | 0x5678; /* equals 0x567C */ 0x1234 ^ 0x5678; /* equals 0x444C */ ~ 0x1234; /* equals 0xEDCB */
&
can also be a pointer reference operator. Refer to Pointers for more information.
Bitwise Shift Operators
Binary operators <<
and >>
move the bits of the left operand by a number of positions specified by the right operand, to the left or right, respectively. Right operand has to be positive.
With shift left (<<
), far left bits are discarded and “new” bits on the right are assigned zeroes. Thus, shifting unsigned operand to the left by n positions is equivalent to multiplying it by 2n if all discarded bits are zero. This is also true for signed operands if all discarded bits are equal to a sign bit.
000001 << 5; /* equals 000040 */ 0x3801 << 4; /* equals 0x8010, overflow! */
With shift right (>>
), far right bits are discarded and the “freed” bits on the left are assigned zeroes (in case of unsigned operand) or the value of a sign bit (in case of signed operand). Shifting operand to the right by n positions is equivalent to dividing it by 2n.
0xFF56 >> 4; /* equals 0xFFF5 */ 0xFF56u >> 4; /* equals 0x0FF5 */
Bitwise vs. Logical
Do not forget of the principle difference between how bitwise and logical operators work. For example:
0222222 & 0555555; /* equals 000000 */ 0222222 && 0555555; /* equals 1 */ ~ 0x1234; /* equals 0xEDCB */ ! 0x1234; /* equals 0 */
What do you think about this topic ? Send us feedback!