Bit Shifting
Bit shifting moves all bits left or right. Left shift multiplies by 2โฟ; right shift divides by 2โฟ. Single-cycle CPU operations โ much faster than multiplication or division.
Why This Mathematical Concept Matters
Why: Bit shifts are single-cycle CPU operations โ much faster than multiplication or division.
How: Left shift: move bits left, fill right with 0. Right: logical fills with 0; arithmetic preserves sign.
- โx << n = x ร 2โฟ. Use instead of Math.pow(2,n) for speed.
- โ(low+high) >> 1 avoids overflow in binary search midpoint.
- โ1 << n creates mask; (x >> n) & 1 extracts bit n.
Bit Shifting โ Multiply and Divide by Powers of 2
Left shift multiplies, right shift divides. Fast, single-cycle CPU operations. Essential for low-level programming.
๐ Examples โ Click to Load
Input Format
Shift Type
โ ๏ธFor educational and informational purposes only. Verify with a qualified professional.
๐งฎ Fascinating Math Facts
Bit shifts are single-cycle CPU operations โ much faster than multiplication.
โ CPU Architecture
RGB colors: (R<<16)|(G<<8)|B โ used in every graphics API.
โ Graphics Programming
๐ Key Takeaways
- โข Left shift (<<) multiplies by 2n โ each position doubles the value
- โข Right shift (>>) divides by 2n โ integer division, truncates remainder
- โข Arithmetic right shift preserves the sign bit for signed integers (negative stays negative)
๐ก Did You Know?
๐ How Bit Shifting Works
Bit shifting moves all bits left or right. Left shift fills new right bits with 0; right shift fills new left bits โ with 0 for logical, or the sign bit for arithmetic.
Left Shift (<<)
5 << 2 = 20. Each position = multiply by 2. 5ร4=20.
Logical Right (>>>)
Always fills with 0. 40 >>> 2 = 10. Good for unsigned.
Arithmetic Right (>>)
Preserves sign. -8 >> 1 = -4. Essential for signed division.
๐ฏ Expert Tips
๐ก Fast Power-of-2 Math
Use x<<n instead of x*Math.pow(2,n). Same result, much faster.
๐ก Midpoint Without Overflow
(low+high)>>1 avoids overflow in binary search.
๐ก Bit Mask Creation
1<<n creates a mask with bit n set. (x>>n)&1 extracts bit n.
๐ก Signed vs Unsigned
Use >> for signed division, >>> for unsigned. JavaScript has both.
โ๏ธ Shift Types Comparison
| Shift Type | Operator | Fill Bits | Use Case |
|---|---|---|---|
| Left | << | 0 (right) | Multiply by 2^n |
| Logical Right | >>> | 0 (left) | Unsigned divide |
| Arithmetic Right | >> | Sign bit (left) | Signed divide |
โ Frequently Asked Questions
What is the difference between logical and arithmetic right shift?
Logical right shift (>>>) always fills new left bits with 0. Arithmetic right shift (>>) copies the sign bit, preserving negative numbers. For positive values both give the same result.
Can bit shifting cause overflow?
Yes. Left shifting can push bits beyond the bit width and they are lost. For example, 1<<31 in 32-bit can overflow. Use appropriate bit widths.
Why use bit shift instead of multiplication?
Bit shifts are single-cycle CPU operations. Multiplication takes multiple cycles. Compilers often convert x*4 to x<<2 automatically.
How does bit shifting work with negative numbers?
Left shift works the same. For arithmetic right shift, the sign bit is propagated. -8>>1 = -4. Logical right shift treats as unsigned.
What are common bit shift tricks?
x<<n = xร2^n, x>>n = xรท2^n, (a+b)>>1 = midpoint, 1<<n = mask for bit n, (x>>n)&1 = extract bit n.
Do all languages implement bit shifting the same?
No. JavaScript has <<, >>, >>>. Python only has << and >> (arithmetic). C/C++ >> behavior on negatives is implementation-defined.
What is a shift-and-mask pattern?
Shift to position then AND with mask. E.g. (color>>16)&0xFF extracts red from packed RGB. Common in graphics and protocols.
When should I use arithmetic vs logical right shift?
Use arithmetic (>>) for signed integers when dividing by powers of 2. Use logical (>>>) for unsigned or when you want zero-fill.
๐ Bit Shifting by the Numbers
๐ Official Data Sources
โ ๏ธ Disclaimer: This calculator provides bit shift results for educational and programming reference. Behavior may vary by language (JavaScript uses 32-bit for bitwise ops). Always verify against your target platform. Not a substitute for official documentation.