Bit Shifting
Concept
Bit Shifting is the act of physically sliding the entire sequence of bits to the left or to the right.
In Base-10 Decimal, if you take the number 15, and shift the digits to the left by adding a zero (150), you just mathematically multiplied the number by 10!
Because computers operate in Base-2 Binary, sliding the bits to the left mathematically multiplies the number by 2. Sliding them to the right mathematically divides the number by 2.
Left Shift (<<)
The Left Shift operator (<<) shifts all bits to the left by a specified number of positions. The empty spaces on the right are filled with 0s.
5 << 1 (Shift 5 left by 1 position).
- Binary for 5:
0000 0101 - Shifted left:
0000 1010(This is the binary for 10). - Mathematical formula: . (
5 * 2^1 = 10).
5 << 3 (Shift 5 left by 3 positions).
- Mathematical formula: .
- Because bit shifting happens instantly in the CPU hardware,
5 << 3executes significantly faster than typing5 * 8.
Right Shift (>> and >>>)
The Right Shift operator shifts all bits to the right. Any bits that fall off the absolute right edge are permanently deleted.
13 >> 1 (Shift 13 right by 1 position).
- Binary for 13:
0000 1101 - Shifted right:
0000 0110(The1on the edge fell off and was deleted). 0000 0110is the binary for 6.- Mathematical formula:
Math.floor(X / 2^Y). (Math.floor(13 / 2) = 6).
The Sign-Propagating Right Shift (>>)
When you shift right, empty spaces open up on the absolute left edge of the binary. What do we fill them with?
The standard >> operator is “Sign-Propagating”. It looks at the Sign Bit (the MSB).
If the number is positive (MSB is 0), it fills the empty spaces with 0s.
If the number is negative (MSB is 1), it fills the empty spaces with 1s! This brilliantly preserves the negative value of the number during division.
The Zero-Fill Right Shift (>>>)
Exclusive to JavaScript and Java, the >>> operator blindly fills the empty left spaces with 0s, regardless of the Sign Bit.
If you use >>> on a Negative number, it shoves a 0 into the Sign Bit column, instantly mutating the massive negative number into a massive positive number!
Interview Strategy
Bit shifting is rarely the entire solution to a problem. It is used as a highly optimized tool inside the solution.
1. Finding the Midpoint:
In Binary Search, the midpoint formula is mid = left + Math.floor((right - left) / 2).
You can write this as: mid = left + ((right - left) >> 1). This is a massive Senior-level flex that proves you understand CPU optimizations.
2. Building Masks:
If a problem requires you to check the i-th bit of a number, you don’t shift the number itself. You build a “Mask” by taking 1 and shifting it left by positions! mask = 1 << i. You then AND the mask against the number.
Interview Questions
Q: A developer uses 1 << 32 in JavaScript to create a massive bitmask. What happens?
A: It fails completely. In JavaScript, bitwise shift operators implicitly wrap around using modulo 32.
1 << 32 evaluates as 1 << (32 % 32), which becomes 1 << 0. The bit doesn’t shift at all! The result is just 1.
Q: Why would you ever use >>> (Zero-Fill Right Shift) instead of >>?
A: >>> is primarily used when dealing with raw binary data streams, colors (Hexadecimal RGB values), or cryptographic hashing. In these domains, the bits represent physical data flags, not mathematical numbers. The “Sign Bit” is just another pixel or flag. If you use >>, the CPU might randomly replicate 1s across your data just because the leading bit happened to be a 1, completely corrupting the image/hash. >>> safely shifts the raw data without trying to do math on it.