Skip to content
Tutorials Center
My learning

Topics

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.

C
unsigned mask = 1u << 5;      /* 32 */
unsigned half = 200u >> 1;    /* 100 */

Try it

C Shifts as multiply, divide and mask-builder

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.

C Right shift, negative numbers and rotation

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

C
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, not 1 << n. A literal 1 is a signed int and gives wrong results or UB for n near 31, and it cannot reach bit 32 at all. Use 1ull << n for 64-bit masks.
  • Never shift by the full width. x << 32 for a 32-bit type is undefined, not zero. Hardware often masks the count, so you may see x come back unchanged. That is why rotl32 masks 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 >> 1 is -4 (rounds toward negative infinity) while -7 / 2 is -3 (toward zero).
  • Precedence: shifts rank below + - and above comparisons. 1 << 2 + 3 is 1 << 5, and x & 1 << n is x & (1 << n). Use parentheses.
  • Small types promote: the shifted value of unsigned char is computed as int, so cast the result back when you need 8 bits.
  • >> on floating-point values is not allowed; use ldexp to 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.