Bit Shift Operation
Shift bits of an integer left or right
Bit Shift Calculator
Bitwise Shifting
This function shifts the bits of an integer left or right. Left means multiplication, right means division by powers of two.
Bit-Shift Visualization
Example: 10100110 << 2 (Left Shift)
Left Shift
(<<)
Multiplication
n << k = n × 2^k
Zeros from rightRight Shift
(>>)
Division
n >> k = n ÷ 2^k
Zeros from leftLogical Shift
- Shifted-out bits are discarded
- Zeros are shifted in (regardless of direction)
- No sign handling
- Ideal for unsigned numbers
Mathematical Foundations of Bit Shifting
Bit shifting is a fundamental operation that has a mathematical relationship to powers of 2:
Left Shift
Each position to the left corresponds to multiplication by 2
Right Shift
Each position to the right corresponds to division by 2 (rounded down)
Bit Shift Formulas and Examples
General Shift Formulas
n = starting value, k = number of shift positions
Step-by-Step Example: 10100110 << 2
Original: 10100110₂ = 166₁₀
Operation: Left shift by 2 positions
Calculation: 166 × 2² = 166 × 4 = 664
Binary Shift:
10100110 → 101001100 → 1010011000
Result: 1010011000₂ = 664₁₀ = 298₁₆
Right Shift Example: 10100110 >> 2
Original: 10100110₂ = 166₁₀
Operation: Right shift by 2 positions
Calculation: ⌊166 ÷ 2²⌋ = ⌊166 ÷ 4⌋ = ⌊41.5⌋ = 41
Binary Shift:
10100110 → 01010011 → 00101001
Result: 00101001₂ = 41₁₀ = 29₁₆
More Shift Examples
Original: 00001000₂ (8)
Result: 01000000₂ (64)
Math: 8 × 2³ = 8 × 8 = 64
Original: 01100100₂ (100)
Result: 00011001₂ (25)
Math: ⌊100 ÷ 4⌋ = 25
Shift Reference
Standard Example
Powers of Two
2⁰ = 1
2¹ = 2
2² = 4
2³ = 8
2⁴ = 16
2⁵ = 32
2⁶ = 64
2⁷ = 128
Shift Operators
<<: Left shift (multiplication)
>>: Right shift (division)
Logical: Fill with zeros
Arithmetic: Preserve sign
Circular: Bits rotate
Common Applications
Fast multiplication: n << k
Fast division: n >> k
Create bit masks: (1 << n) - 1
Alignment: (addr + 7) >> 3 << 3
Bitwise Shifting - Detailed Description
Bitwise Shifting
In bitwise shifting, the binary digits are shifted by a specified number of bit positions to the left or right. The direction specification is always understood in the standard convention of the binary system, regardless of computer architecture: left means multiplication and right means division by a power of two.
• Left shift: Multiplication by 2^n
• Right shift: Division by 2^n
• Very fast operation
• Bits are shifted out and filled in
Registers and Hardware
Processor registers contain a defined finite number of bits, which is why the specified number of bits are "shifted out" at one end of the register, while the same number are "shifted in" ("pulled in") at the other end.
Hardware Implementation
1. Bits are physically shifted
2. Empty positions are filled
3. Overflowing bits are lost
4. Operation occurs in one clock cycle
Logical Shifting
In the logical shifting used here, the shifted-out bits are discarded and zeros are filled in, regardless of the shift direction. Unlike arithmetic shifts, signed numbers cannot be used.
• Always fill with zeros
• No sign handling
• Suitable for unsigned numbers
• Simple implementation
Mathematical Applications
Bit shifting is one of the most efficient methods for multiplication and division by powers of two. It is frequently used in systems programming, graphics processing, and performance-critical applications.
Performance Benefits
- Extremely fast (1 clock cycle)
- No expensive multiplications/divisions
- Ideal for powers of 2
- Often optimized by compiler
Practical Bit Shift Examples
Array Indexing
Scenario: 4-byte alignment
Index: 5
Address: 5 << 2 = 20
Effect: index × 4 (faster than *4)
Bit Masks
Scenario: Create n-bit mask
Operation: (1 << n) - 1
Example: (1 << 4) - 1 = 15
Result: 00001111₂ (4-bit mask)
Memory Alignment
Scenario: 8-byte alignment
Address: 0x12345678
Alignment: (addr + 7) >> 3 << 3
Result: Next 8-byte boundary
Programming Tips
- Fast multiplication: n << k instead of n * (1<
- Division by 2^n: value >> n
- Check bit position: (value >> bit) & 1
- Watch overflow: Bits are lost
- Compiler optimization: x*8 → x<<3
- Portability: Consider shift behavior
|
|