Left shift
A left shift moves every bit to a higher position.
The empty low positions fill with 0.
A shift of one position multiplies the value by 2.
Try the left shift in the bit editor
The result of 1 shifted left by 4 positions. Edit the expression or select a bit to flip it.
7
6
5
4
3
2
1
0
16
value · 8 bits · unsigned
- hex
- 0x10
Bit width
Shift
The left shift step by step
Shift 1 left by four positions:
- Write the value: 00000001.
- Move every bit four positions to the left.
- Fill the four low positions with 0.
- The result is 00010000, which is 16.
The value 1 shifted left
| Expression | Binary | Decimal |
|---|---|---|
| 1 << 0 | 00000001 | 1 |
| 1 << 1 | 00000010 | 2 |
| 1 << 2 | 00000100 | 4 |
| 1 << 3 | 00001000 | 8 |
| 1 << 4 | 00010000 | 16 |
| 1 << 5 | 00100000 | 32 |
| 1 << 6 | 01000000 | 64 |
| 1 << 7 | 10000000 | 128 |
Where the left shift is used
The expression 1 << n gives a mask with only bit n set. This is the most common use.
A left shift places a field at the correct bit position before an OR.
A shift is cheaper than a multiplication on a processor without a multiplier.
The left shift: points to note
- The register drops a bit that moves past the top. A shift back does not restore it.
- A left shift of 1 by 8 positions gives 0 in an 8-bit register, not 256.
- A shift by a number of positions equal to or larger than the bit width gives undefined behaviour in C.
The full bit editor
The full editor holds many values, evaluates expressions across them, and reads live values from a serial port.
Open this value in the full editorQuestions about the left shift
- What is 1 shifted left by 4?
- The result is 16. The single set bit moves from position 0 to position 4.
- Is a left shift a multiplication by two?
- Yes, for each position, until the register drops a bit at the top.
- Why do drivers write 1 << n?
- The expression builds the mask for bit n without a table of constants.