Skip to content

Bit Shift Calculator: Left, Right and Rotate

Left shift, arithmetic and logical right shift, and bit rotation on 8- to 64-bit integers, in decimal, binary and hex.

Your details

Width
Integer type
Input written in

Decimal may be negative for signed types. Hex and binary are bit patterns; prefixes 0x and 0b are recognised.

Results update as you type

Result (decimal)

−19

Result (binary)
1110 1101
Result (hex)
ED
Operand bits
1011 0101
Operand (decimal)
−75
Last bit out (carry flag)
0

Bits shifted out on the right are discarded, so the result is the quotient rounded down; division in C or Java would round towards zero instead.

Step-by-step solution
  1. Operand: 1011 0101 = −75
  2. Arithmetic shift right by 2: bits move towards the bottom and copies of the sign bit enter on the left.
  3. Result: 1110 1101 = −19
  4. Arithmetic meaning: ⌊−75 ÷ 2²⌋ = −19 (rounds towards −∞)

Bit shifts are among the cheapest operations a processor can perform, which is why they appear everywhere in low-level code: multiplying or dividing by powers of two, packing flags into a byte, extracting colour channels, building hash functions or talking to hardware registers. They are also a classic source of bugs, because a shift does not behave the same way on signed and unsigned numbers, and every language handles out-of-range counts differently.

This bit shift calculator shows exactly what happens to every bit. Pick a width (8, 16, 32 or 64 bits), say whether the integer is signed (two’s complement) or unsigned, type the number in decimal, hex or binary, and choose one of five operations: left shift (<<), **arithmetic right shift** (>>), logical right shift (>>>), rotate left (ROL) or rotate right (ROR).

The result appears in decimal, as a binary pattern padded to the full width and grouped in fours, and in hexadecimal. The calculator also reports the last bit shifted out (what the x86 carry flag would hold), flags overflow on left shifts and explains when a right shift rounds a negative number down instead of towards zero. All the arithmetic uses arbitrary-precision integers, so 64-bit values are exact.

How to use this calculator

  1. Choose the operation: shift left, arithmetic or logical shift right, or rotate.
  2. Choose the width and whether the type is signed or unsigned, as in your program (int8_t, uint16_t, int, uint64_t…).
  3. Say how you will type the number: decimal (negative values allowed for signed types), hex or binary bit patterns. Prefixes 0x and 0b work too.
  4. Enter the shift count.
  5. Read the result in decimal, binary and hex. The steps show the bits before and after, and the notes warn about overflow, lost bits and counts larger than the width.

The formula

For a ww-bit pattern xx and a count nn:

x≪n=(x⋅2n) mod 2wx≫logicaln=⌊xunsigned2n⌋x≫arithn=⌊xsigned2n⌋x \ll n = (x \cdot 2^{n}) \bmod 2^{w} \qquad x \gg_{\text{logical}} n = \left\lfloor \frac{x_{\text{unsigned}}}{2^{n}} \right\rfloor \qquad x \gg_{\text{arith}} n = \left\lfloor \frac{x_{\text{signed}}}{2^{n}} \right\rfloor

The arithmetic shift copies the sign bit into the vacated positions, so its result is the signed quotient rounded towards minus infinity. Rotations lose no bit; with k=n mod wk = n \bmod w:

ROL(x,n)=((x≪k)  ∣  (x≫(w−k))) mod 2w\text{ROL}(x, n) = \big((x \ll k) \;|\; (x \gg (w - k))\big) \bmod 2^{w}

and ROR is the mirror image. A left shift overflows when x⋅2nx \cdot 2^n falls outside the range of the type: −2w−1-2^{w-1} to 2w−1−12^{w-1} - 1 for signed, 00 to 2w−12^w - 1 for unsigned.

Worked example

Take the signed 8-bit value −75, stored as 1011 0101 (hex B5).

  • Arithmetic shift right by 2: 1110 1101 = −19. Two copies of the sign bit enter on the left; the result is ⌊−75/4⌋=−19\lfloor -75 / 4 \rfloor = -19, not the −18 that integer division in C would give.
  • Logical shift right by 2: 0010 1101 = 45. Zeros enter on the left, so the sign is lost.
  • Shift left by 2: 1101 0100 = −44. The true product is −300, which does not fit in 8 bits: overflow.
  • Rotate left by 3: 1010 1101 (hex AD) = −83 signed or 173 unsigned. The three top bits 101 reappear on the right.

Shift operators in common languages

Language Left shift Right shift on signed Right shift on unsigned Count ≥ width
C / C++ << (overflow of a signed value is undefined before C++20) >> is arithmetic on mainstream compilers (C23 leaves it implementation-defined) >> is logical undefined behaviour
Java << >> arithmetic >>> logical (there is no unsigned type) count taken mod 32 (int) or mod 64 (long)
JavaScript << on 32-bit integers >> arithmetic >>> logical, result unsigned count taken mod 32
Python << on unbounded integers (never overflows) >> arithmetic (floor) — no limit
x86 assembly SHL / SAL SAR SHR count masked to 5 bits (6 for 64-bit)

Rotations have no operator in C, Java or JavaScript; compilers recognise the idiom (x << k) | (x >> (w − k)), and C++20 offers std::rotl and std::rotr.

Shifting instead of multiplying or dividing

A left shift by nn multiplies by 2n2^n as long as nothing overflows, and a right shift divides by 2n2^n. Compilers already apply this optimisation for you, so write the multiplication when you mean arithmetic. The trap is negative numbers: an arithmetic right shift rounds down (−75 >> 2 = −19), whereas integer division in C, C++, Java and JavaScript truncates towards zero (−75 / 4 = −18). Python’s // floor division, on the other hand, matches the shift.

Frequently asked questions

What is the difference between arithmetic and logical right shift?

Both move the bits towards the least significant end. A logical shift fills the vacated top bits with zeros; an arithmetic shift fills them with copies of the sign bit, so a negative number stays negative. On unsigned values they give the same result only when the top bit is 0.

Why does −1 >>> 1 give a huge positive number?

Because the logical shift treats the bits of −1 (all ones) as an unsigned pattern and inserts a zero at the top. In 32 bits the result is 0111…1 = 2,147,483,647. That is the behaviour of >>> in Java and JavaScript.

What happens to the bits that are shifted out?

They are discarded. On x86 the last one lands in the carry flag (CF), which this calculator also shows. Rotations do not lose bits: they wrap around to the other end.

When does a left shift overflow?

When the product x × 2ⁿ does not fit in the type. For a signed type it also happens when a 1 reaches the sign bit, turning a positive number negative, as in 64 << 1 = −128 in 8 bits.

Can I shift by more than the width of the type?

Mathematically every bit falls out, and that is what the calculator shows. Real hardware and languages differ: x86 and Java take the count modulo 32 or 64, JavaScript modulo 32, and C or C++ treat it as undefined behaviour, so avoid it in portable code.

Sources

Last updated:

More Math calculators

People also use