Bit Shift Calculator
Enter any integer, choose the shift direction and number of positions, and see the result in binary, decimal, hex, and octal instantly. You can pick between logical right shift (fills with zeros) and arithmetic right shift (preserves the sign bit), choose your bit width (8, 16, or 32), and get a full step-by-step explanation of exactly which bits moved where.
What is a bit shift?
A bit shift moves every bit in a binary number a certain number of positions to the left or right. Bits that fall off the end of the register are discarded, and the positions that open up on the other side are filled with either zeros or, in the case of arithmetic right shift, a copy of the original sign bit. Bit shifts are one of the cheapest operations a CPU can perform and are used everywhere from graphics processing to cryptography to compiler optimizations.
Left shift vs. right shift
A left shift by n positions is mathematically identical to multiplying by 2^n, provided the result fits within the register. For example, 13 << 2 gives 52 because 13 * 4 = 52. A right shift by n positions is integer division by 2^n. There are two right-shift variants. Logical right shift (>>>) always inserts zeros on the left, treating the number as unsigned. Arithmetic right shift (>>) copies the sign bit (the most significant bit) into the vacated positions, which correctly halves a negative two's complement integer: -4 >> 1 gives -2, not 126.
Signed vs. unsigned interpretation
Modern computers store integers using two's complement notation for signed types. In an 8-bit register, bit patterns 10000000 through 11111111 represent the values -128 through -1 when the type is signed, but 128 through 255 when unsigned. This calculator shows both interpretations side by side so you can see the effect a shift has regardless of how the host language declares the type. Languages like C treat the sign of a right shift as implementation-defined for signed types, while JavaScript's >>> operator always performs a logical (unsigned) shift.
Practical uses of bit shifting
Bit shifts appear constantly in low-level and systems programming. Common patterns include fast powers of 2 (x << 3 is always cheaper than x * 8), reading and writing hardware registers bit by bit, packing multiple small values into a single integer (bit fields), generating bitmasks (1 << n sets bit n), and checking whether a specific bit is set (x & (1 << n)). In cryptographic hash functions such as SHA-256 shifts and rotations are the primary mixing operations. Understanding what happens at the bit level makes debugging unexpected negative values, overflow, and mask logic much easier.
Bit shift operations at a glance
| Operation | Symbol | Fill bits | Effect | Example |
|---|---|---|---|---|
| Left shift | << | Zeros on right | Multiply by 2 per step | 13 << 2 = 52 |
| Logical right shift | >>> | Zeros on left | Unsigned divide by 2 per step | 52 >>> 2 = 13 |
| Arithmetic right shift | >> | Sign bit on left | Signed divide by 2 per step | -4 >> 1 = -2 |
| Overflow (left) | << | n/a | High bits discarded by register width | 200 << 2 = 800 -> 32 (8-bit) |
| Underflow (right) | >>> | n/a | Low bits lost permanently | 1 >>> 1 = 0 |
All results are for an 8-bit register unless noted.
Frequently asked questions
Why does left-shifting a number sometimes give a negative result?
When you left shift a value far enough, the result bit pattern sets the most significant bit (the sign bit) to 1. In a signed integer type that is interpreted as a negative two's complement value. For example, 64 (01000000 in 8 bits) shifted left by 1 becomes 128, but in signed 8-bit arithmetic the pattern 10000000 equals -128. This is overflow, and whether it is defined behavior depends on the language - in C it is undefined for signed types, while in JavaScript all bitwise operators work on 32-bit signed integers.
What is the difference between >> and >>> in programming?
In languages like Java and JavaScript, >> is an arithmetic right shift that copies the sign bit into vacated positions, preserving the sign of a negative number. >>> is a logical (unsigned) right shift that always inserts zeros, ignoring any sign. For positive numbers both produce the same result. For negative numbers, >> keeps the result negative (halving it), while >>> produces a large positive number because the sign bit is replaced with 0.
Can a bit shift replace multiplication or division?
Yes, for powers of 2. x << n equals x * 2^n and x >>> n equals floor(x / 2^n) for unsigned values. Modern compilers do this substitution automatically when optimizing, so you rarely need to write shifts by hand for performance. The main reasons to write explicit shifts in application code are clarity (showing you are working at the bit level) or when implementing protocols and data structures that explicitly pack values into bit fields.
What happens to bits that are shifted out of the register?
They are discarded. In most programming languages and CPUs there is no way to retrieve them once the operation is complete. Some processor instruction sets expose a carry flag that captures the last bit shifted out, and languages like Rust provide rotate_left and rotate_right functions that wrap the shifted-out bits back into the other end instead of discarding them (circular shift).
What bit width should I use?
Choose the width that matches your target data type: 8 bits for a byte or uint8_t, 16 bits for a short or uint16_t, and 32 bits for an int, long, or uint32_t in most C-family languages. JavaScript bitwise operators always work on 32-bit integers. The bit width matters because it determines when overflow occurs and how the signed range is interpreted.