Bit Shifting

In short: A bit shift moves every bit in a number's binary representation left or right by a set number of places. Shifting left by k multiplies a number by 2^k, and shifting a non-negative number right by k divides it by 2^k, rounding down. A logical right shift fills in 0s, while an arithmetic right shift copies the sign bit so negative numbers stay negative.

A bit shift moves each digit in a number's binary representation left or right. Shifting left by k places multiplies a number by 2^k (as long as no 1 bits fall off the end), and shifting a non-negative number right by k places divides it by 2^k, rounding down.

There are three main types of shifts. Here's how they compare:

Left shift Logical right shift Arithmetic right shift
In Java << >>> >>
Bit that falls off The most significant (leftmost) bit The least significant (rightmost) bit The least significant (rightmost) bit
Bit added A 0 on the right A 0 on the left A copy of the most significant bit (the sign bit) on the left
Shifting by 1 Multiplies by 2 Divides a non-negative number by 2, rounding down Divides by 2, rounding down, and keeps the sign
Example (4 bits) 0011 → 0110 1011 → 0101 1011 → 1101

On fixed-size integers, a shift is a single, super fast processor instruction—O(1) time. That's why compilers often turn multiplying by a power of 2 into a left shift behind the scenes.

Left Shifts

When shifting left, the most-significant bit is lost, and a 0 bit is inserted on the other end.

The left shift operator is usually written as "<<".

0010 << 1 → 0100 0010 << 2 → 1000

A single left shift multiplies a binary number by 2:

0010 << 1 → 0100 0010 is 2 0100 is 4

Logical Right Shifts

When shifting right with a logical right shift, the least-significant bit is lost and a 0 is inserted on the other end.

1011 >>> 1 → 0101 1011 >>> 3 → 0001

For positive numbers, a single logical right shift divides a number by 2, throwing out any remainders.

0101 >>> 1 → 0010 0101 is 5 0010 is 2

Arithmetic Right Shifts

When shifting right with an arithmetic right shift, the least-significant bit is lost and the most-significant bit is copied.

Languages handle arithmetic and logical right shifting in different ways. Java provides two right shift operators: >> does an arithmetic right shift and >>> does a logical right shift.

1011 >> 1 → 1101 1011 >> 3 → 1111 0011 >> 1 → 0001 0011 >> 2 → 0000

The first two numbers had a 1 as the most significant bit, so more 1's were inserted during the shift. The last two numbers had a 0 as the most significant bit, so the shift inserted more 0's.

If a number is encoded using two's complement, then an arithmetic right shift preserves the number's sign, while a logical right shift makes the number positive.

// Arithmetic shift 1011 >> 1 → 1101 1011 is -5 1101 is -3 // Logical shift 1111 >>> 1 → 0111 1111 is -1 0111 is 7

Frequently Asked Questions

What does bit shifting do?

Bit shifting moves the bits in a number left or right. The '<<' operator shifts left (filling with zeros) and '>>' shifts right. Each left shift by one doubles the value; each right shift by one halves it, rounding down.

What's the difference between a left shift and a right shift?

A left shift (<<) moves bits toward the most significant end and multiplies by powers of two; a right shift (>>) moves bits toward the least significant end and divides by powers of two.

What's the difference between a logical and an arithmetic right shift?

A logical right shift fills the vacated high bits with 0s, so the result is never negative. An arithmetic right shift copies the sign bit into them, so negative numbers stay negative. In Java and JavaScript, >> is arithmetic and >>> is logical; in Python, >> is always arithmetic.

Is a left shift the same as multiplying by 2?

Shifting left by k multiplies by 2^k as long as the result fits in the integer type. With fixed-size integers, bits shifted past the most significant position are lost, so big values overflow. Python's integers grow as needed, so nothing is lost there.

What is bit shifting used for?

Fast multiplication and division by powers of two, packing several values into one integer, building bitmasks, and low-level work like hashing and graphics.

Last updated: October 21, 2026

. . .