Developer Bitwise

Bit shift calculator

Value is in
Value
Shift by
Direction
Shifted 40
Shifting left by n multiplies by 2ⁿ
Both directions
Equivalent to × 8
In decimal 40
In binary 0010 1000
In hex 0x28
Shifted left 40
Shifted right (logical) 0
Shifted right (arithmetic) 0
Left × 2ⁿ · right ÷ 2ⁿ, truncating

32-bit operations. Bits shifted past the ends are discarded, not wrapped.

Advertisement
320 × 100

Shifting left by n multiplies by 2 to the n; shifting right divides and truncates. 5 shifted left by 3 is 40. Bits pushed past the ends are discarded, so shifting is only equivalent to multiplying while nothing overflows.

How to use bit shifts

1 Enter the value and how many places to shift.
2 Choose left or right.
3 Read the binary row to see the bits move.
4 Note the difference between logical and arithmetic right shifts for negatives.

The two right shifts differ only for negative numbers, and the difference matters. A logical right shift fills from the left with zeros, so a negative number becomes a large positive one. An arithmetic right shift copies the sign bit, preserving the sign and behaving like division by a power of two. Most languages spell these differently — in JavaScript, >> is arithmetic and >>> is logical — and picking the wrong one is a classic source of bugs in code handling signed values.

Questions

Multiplies by two for each place shifted, as long as nothing overflows off the top.

Advertisement
300 × 250
Was this tool any good?
INTERNAL SIGNAL ONLY · WE USE IT TO FIND TOOLS WORTH REBUILDING