The shift operators, << and >>, move the bits of an integer left or right. A left shift by one place doubles a non-negative value, as long as the result still fits in the type. A right shift by one place halves it and drops the lowest bit. Unlike the unary operators, which take one operand, shifts need a value on each side, such as 5 << 1.
The four forms at a glance
| Form | What it does | Example | Result |
|---|---|---|---|
x << n |
Moves the bits of x left by n places |
5 << 1 |
10 |
x >> n |
Moves the bits of x right by n places |
10 >> 1 |
5 |
x <<= n |
Shifts x left and stores the result in x |
x is 3, then x <<= 2 |
x becomes 12 |
x >>= n |
Shifts x right and stores the result in x |
x is 12, then x >>= 2 |
x becomes 3 |
The plain forms leave their left operand alone. 5 << 1 produces 10, and the 5 is still 5 afterward. Only the compound forms store a new value, following the same pattern as the other C assignment operators, such as +=.
Left shift with <<
In a left shift, every bit moves up by n places, and zeros fill the empty places on the right. For unsigned types, bits pushed past the top are lost. Signed overflow is undefined, as the section on undefined shifts explains.
#include <iostream>
int main() {
int a = 3; // binary 0011
std::cout << (a << 2) << "\n"; // binary 1100, prints 12
std::cout << a << "\n"; // a is unchanged, prints 3
return 0;
}
Output:
12
3
The parentheses around a << 2 are not optional inside a stream expression. The streams section below explains why.
Right shift with >>
In a right shift, every bit moves down by n places, and the bits that fall off the bottom are discarded.
#include <iostream>
int main() {
int b = 12; // binary 1100
std::cout << (b >> 2) << "\n"; // binary 0011, prints 3
return 0;
}
What fills the empty places on the left depends on the type:
- For unsigned types, zeros always fill the left. For example,
0xF0u >> 4gives0x0F, which is 15. - For negative signed values, the standard leaves the result to the implementation before C++20. Most compilers copy the sign bit into the empty places, so
-7 >> 1gives -4. Division rounds toward zero instead, so-7 / 2gives -3. Don’t use>>as a stand-in for/on negative numbers.
Compound forms: <<= and >>=
The compound forms combine a shift with assignment. x <<= 2 is the same as x = x << 2.
#include <iostream>
int main() {
int x = 3;
x <<= 2; // same as x = x << 2
std::cout << x << "\n"; // prints 12
x >>= 1; // same as x = x >> 1
std::cout << x << "\n"; // prints 6
return 0;
}
Output:
12
6
The left operand is evaluated only once. So values[next()] <<= 2 calls next() one time, while values[next()] = values[next()] << 2 calls it twice.
Signed and unsigned values
The signed vs unsigned in C distinction matters more for bit work than for ordinary arithmetic, because shifts act on the bit pattern itself, including the sign bit.
Unsigned types are the safe choice. For an unsigned left operand and a shift count in range, the result is always defined. Bits that move past the top are dropped, so the value wraps around. Signed types have more restrictions, and the next section lists the cases that are undefined before C++20.
Shift counts that are undefined
Some shifts are undefined behavior, which means the standard does not say what happens. A program can seem to work on one compiler and fail on another. These cases are undefined:
- A negative shift count, such as
1 << -1. - A shift count equal to or greater than the width of the promoted left operand. On a platform where
intis 32 bits,1 << 32is undefined. - Shifting a negative signed value to the left, before C++20.
- Shifting a signed value so the result does not fit, such as
1 << 31on a 32-bitint, before C++20.
The width of int depends on the platform. sizeof(int) * CHAR_BIT gives the width in bits, and the usual sizes are covered in primitive data types in C.
#include <iostream>
int main() {
int n = 40;
// long long bad = 1 << n; // undefined: 40 is too large for int
long long good = 1LL << n; // 1099511627776
unsigned int bit = 1u << 31; // defined: unsigned values wrap around
std::cout << good << "\n";
std::cout << bit << "\n";
return 0;
}
On a platform where unsigned int is 32 bits, which covers most current systems, the output is:
1099511627776
2147483648
For values wider than int, use a wider type such as long long, as explained in what is long long in C.
Precedence: when to use parentheses
Shifts bind more tightly than the comparison operators, but less tightly than + and -. So x << 1 == 4 means (x << 1) == 4, and 1 << n + 1 means 1 << (n + 1).
#include <iostream>
int main() {
int n = 1;
std::cout << (1 << n + 1) << "\n"; // prints 4, parsed as 1 << (n + 1)
std::cout << ((1 << n) + 1) << "\n"; // prints 3
return 0;
}
Shifts also sit above &, ^, and |, so flags & 1 << 2 means flags & (1 << 2). The rule is correct, but parentheses make the intent clear, and they cost nothing.
The same symbols in streams
In C++, << and >> also do input and output: std::cout << x and std::cin >> x. These are overloaded operators that call stream functions, not bit shifts. Stream insertion has the same precedence as the shift and is read left to right, so a shift inside a stream expression needs parentheses.
#include <iostream>
int main() {
std::cout << 1 << 1 << "\n"; // prints 11
std::cout << (1 << 1) << "\n"; // prints 2
return 0;
}
Practical uses: bit masks and packing
Shifts are most useful when you work with individual bits. A mask built as 1u << n picks out bit n. Combine masks with the bitwise operators, such as bitwise xor C, to set, clear, or flip bits.
#include <iostream>
int main() {
const unsigned int READ = 1u << 0; // binary 001
const unsigned int WRITE = 1u << 1; // binary 010
const unsigned int EXECUTE = 1u << 2; // binary 100
unsigned int permissions = READ | WRITE;
permissions &= ~WRITE; // clear the WRITE flag
std::cout << "read: " << ((permissions & READ) != 0) << "\n";
std::cout << "write: " << ((permissions & WRITE) != 0) << "\n";
std::cout << "execute: " << ((permissions & EXECUTE) != 0) << "\n";
return 0;
}
Output:
read: 1
write: 0
execute: 0
Shifts can also pack several small values into one integer. This example puts three 8-bit color channels into one 24-bit value:
#include <iostream>
int main() {
unsigned int red = 0x12;
unsigned int green = 0x34;
unsigned int blue = 0x56;
unsigned int color = (red << 16) | (green << 8) | blue; // 0x123456
std::cout << std::hex << color << "\n"; // prints 123456
return 0;
}
Shifts can also stand in for multiplying or dividing by a power of two. For plain arithmetic, * and / are clearer, and compilers often generate the same machine code for constant values.
Small types are promoted first
Operands of types smaller than int, such as char and short, are promoted to int before a shift runs. The shift itself is computed as an int, so the result can be larger than the variable can hold until it is stored back.
#include <iostream>
int main() {
unsigned char c = 200; // binary 11001000
c = c << 1; // computed as int 400, stored as 144
std::cout << static_cast<int>(c) << "\n"; // prints 144
return 0;
}
On most platforms, an unsigned char holds values from 0 to 255, so 400 wraps around to 144 when it is stored.
Checklist
- Use unsigned types for bit manipulation.
- Keep shift counts from 0 up to one less than the width of the promoted left operand.
- Add parentheses when a shift shares an expression with arithmetic, comparisons, bitwise operators, or a stream.
- Check which C++ standard your compiler uses, since C++20 changed the rules for some signed shifts.