C Reference ยท Operators and Expressions
Shift operators in C
Reference for the C shift operators << and >>: multiplication and division by powers of two, shift-count limits, signed shifts, promotion and portable rotates.
The shift operators move the bits of an integer. x << n shifts the bits of x left by n positions and fills with zeros; x >> n shifts right. For non-negative values, a left shift by n multiplies by 2^n and a right shift divides by 2^n (rounding down).
Shifts are cheap and common in masks, hashing and packing fields into words. They also have more undefined and implementation-defined cases than any other simple operator, so it pays to know the rules: the count must be in range, the left operand is promoted, and signed values need care.
unsigned mask = 1u << 5; /* 32 */ unsigned half = 200u >> 1; /* 100 */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
value << count value >> count
| Part | Description |
|---|---|
value |
An integer expression. It undergoes integer promotion; the result has the promoted type of value (the type of count is irrelevant). |
count |
An integer expression, also promoted. It must be non-negative and smaller than the width of the promoted value. |
Result: << shifts in zero bits at the right. >> shifts in zeros for unsigned (and non-negative signed) values; for negative signed values, the result is implementation-defined (usually the sign bit is copied).
Errors / undefined behavior: undefined if count is negative or at least the bit width of the promoted left operand (1u << 32 on 32-bit unsigned). Left-shifting a negative signed value is undefined, and so is left-shifting a signed value when the mathematical result does not fit in the type (1 << 31 for a 32-bit int). Use 1u << 31.
Usage notes
- Use unsigned operands for bit manipulation:
1u << n, not1 << n. A literal1is a signedintand gives wrong results or UB fornnear 31, and it cannot reach bit 32 at all. Use1ull << nfor 64-bit masks. - Never shift by the full width.
x << 32for a 32-bit type is undefined, not zero. Hardware often masks the count, so you may seexcome back unchanged. That is whyrotl32masks its counts. - Left shift is multiplication only while the result fits. Bits shifted past the top are lost.
- Right shift of a signed negative number is not division.
-7 >> 1is-4(rounds toward negative infinity) while-7 / 2is-3(toward zero). - Precedence: shifts rank below
+ -and above comparisons.1 << 2 + 3is1 << 5, andx & 1 << nisx & (1 << n). Use parentheses. - Small types promote: the shifted value of
unsigned charis computed asint, so cast the result back when you need 8 bits. >>on floating-point values is not allowed; useldexpto scale by powers of two.- Rotation has no operator. Combine two shifts as shown; GCC and Clang compile the pattern into one rotate instruction.
- Compound forms
<<=and>>=are listed in assignment operators.