Next: Bitwise Operations, Previous: Numeric Comparisons, Up: Arithmetic [Contents][Index]
Shifting an integer means moving the bit values to the left or right within the bits of the data type. Shifting is defined only for integers. Here’s the way to write it:
/* Left shift. */ 5 << 2 ⇒ 20 /* Right shift. */ 5 >> 2 ⇒ 1
The left operand is the value to be shifted, and the right operand
says how many bits to shift it (the shift count). The left
operand is promoted (see Operand Promotions), so shifting never
operates on a narrow integer type; it’s always either int
or
wider. The value of the shift operator has the same type as the
promoted left operand.
• Bits Shifted In | How shifting makes new bits to shift in. | |
• Shift Caveats | Caveats of shift operations. | |
• Shift Hacks | Clever tricks with shift operations. |