The expression !x has the value 0. To see the value of ~x, you write it in binary form: 00000011. Then you convert each 0 to 1 and each 1 to 0. This produces the value 11111100, which in base 10 is the value 252. (Figure E.3 shows a 16-bit example.) The new value is termed the
Figure E.3. The bitwise negation operator.
The bitwise OR operator (|) combines two integer values to produce a new integer value. Each bit in the new value is set to 1 if one or the other, or both, of the corresponding bits in the original values is set to 1. If both corresponding bits are 0, then the final bit is set to 0 (see Figure E.4).
Figure E.4. The bitwise OR operator.
Table E.1 summarizes how the | operator combines bits.
Table E.1. The Value of b1 | b2
The |= operator combines the bitwise OR operator with assignment:
a |= b; // set a to a | b
The bitwise XOR operator (^) combines two integer values to produce a new integer value. Each bit in the new value is set to 1 if one or the other, but not both, of the corresponding bits in the original values is set to 1. If both corresponding bits are 0 or both are 1, the final bit is set to 0 (see Figure E.5).
Figure E.5. The bitwise XOR operator.
Table E.2 summarizes how the ^ operator combines bits.
Table E.2. The Value of b1 ^ b2
The ^= operator combines the bitwise XOR operator with assignment:
a ^= b; // set a to a ^ b
The bitwise AND operator (&) combines two integer values to produce a new integer value. Each bit in the new value is set to 1 only if both of the corresponding bits in the original values are set to 1. If either or both corresponding bits are 0, the final bit is set to 0 (see Figure E.6).
Figure E.6. The bitwise AND operator.
Table E.3 summarizes how the & operator combines bits.
Table E.3. The Value of b1 & b2
The &= operator combines the bitwise AND operator with assignment:
a &= b; // set a to a & b
Alternative Representations of Bitwise Operators
C++ provides alternative representations of several bitwise operators, as shown in Table E.4. They are provided for locales that do not have the traditional bitwise operators as part of their character sets.
Table E.4. Bitwise Operator Representations
These alternative forms let you write statements like the following:
b = compl a bitand b; // same as b = ~a & b;
c = a xor b; // same as c = a ^ c;
A Few Common Bitwise Operator Techniques
Often controlling hardware involves turning particular bits on or off or checking their status. The bitwise operators provide the means to perform such actions. We’ll go through the methods quickly.
In the following examples, lottabits represents a general value, and bit represents the value corresponding to a particular bit. Bits are numbered from right to left, beginning with bit 0, so the value corresponding to bit position n is 2n. For example, an integer with only bit number 3 set to 1 has the value 23 or 8. In general, each individual bit corresponds to a power of 2, as described for binary numbers in Appendix A. So we’ll use the term
Turning a Bit On
The following two operations each turn on the bit in lottabits that corresponds to the bit represented by bit:
lottabits = lottabits | bit;
lottabits |= bit;
Each sets the corresponding bit to 1, regardless of the former value of the bit. That’s because ORing 1 with either 0 or 1 produces 1. All other bits in lottabits remain unaltered. That’s because ORing 0 with 0 produces 0, and ORing 0 with 1 produces 1.
Toggling a Bit
The following two operations each toggle the bit in lottabits that corresponds to the bit represented by bit. That is, they turn the bit on if it is off, and they turn it off if it is on:
lottabits = lottabits ^ bit;
lottabits ^= bit;
XORing 1 with 0 produces 1, turning an off bit on, and XORing 1 with 1 produces 0, turning an on bit off. All other bits in lottabits remain unaltered. That’s because XORing 0 with 0 produces 0, and XORing 0 with 1 produces 1.
Turning a Bit Off
The following operation turns off the bit in lottabits that corresponds to the bit represented by bit:
lottabits = lottabits & ~bit;