Bitwise Operators

The bitwise operators operate upon the bits of integer values. For example, the left-shift operator moves bits to the left, and the bitwise negation operator turns each one to a zero, and each zero to a one. Altogether, C++ has six such operators: <<, >>, ~, &, |, and ^.

The Shift Operators

The left-shift operator has the following syntax:

						value << shift
					

Here value is the integer value to be shifted, and shift is the number of bits to shift. For example:

13 << 3

means shift all the bits in the value 13 three places to the left. The vacated places are filled with zeros, and bits shifted past the end are discarded (see Figure E.1).

Figure E.1. The left-shift operator.

Because each bit position represents a value twice ...

Get The Waite Group's C++ Primer Plus, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.