Perform a logical binary shift on a positive 8-bit binary integer and understand the effect
📚 Data Representation: Logical Binary Shift
What is a Logical Binary Shift?
A logical shift moves every bit in a binary number left or right, filling the empty positions with zeros. Think of it like sliding a row of beads on a bracelet: when you push the beads to the right, new empty spots appear on the left and are filled with zeros. This operation is used in many computer algorithms to quickly multiply or divide by powers of two.
Shifting Left (Logical Shift Left)
When you shift left by one position, every bit moves one place to the left, and a zero is inserted on the right. Mathematically, shifting left by one is equivalent to multiplying by 2 (for unsigned numbers). Example: $00101101_2 \;\xrightarrow{\text{LSL 1}}\; 01011010_2$
Shifting Right (Logical Shift Right)
When you shift right by one position, every bit moves one place to the right, and a zero is inserted on the left. This is equivalent to integer division by 2 (discarding the remainder). Example: $01011010_2 \;\xrightarrow{\text{LSR 1}}\; 00101101_2$
Step‑by‑Step Example
- Start with an 8‑bit unsigned number: $00101101_2$ (decimal 45).
- Choose a shift direction: let’s shift left by 2 bits.
- Move each bit two places left:
Original: 0 0 1 0 1 1 0 1
After shift: 1 0 1 1 0 1 0 0 - Insert zeros into the vacated rightmost positions.
- Resulting binary: $10110100_2$ (decimal 180).
- Check the math: $45 \times 2^2 = 45 \times 4 = 180$ – it matches!
Quick Reference Table
| Original (8‑bit) | Shifted Left by 1 | Shifted Right by 1 | Effect on Decimal Value |
|---|---|---|---|
| $00101101_2$ (45) | $01011010_2$ (90) | $00010110_2$ (22) | ×2, ÷2 (floor) |
| $11110000_2$ (240) | $11100000_2$ (224) | $01111000_2$ (120) | ×2, ÷2 (floor) |
Why Does the Decimal Value Change?
Each bit position represents a power of two. When you shift left, you effectively add a zero to the least significant bit, which is like adding a factor of 2 to the whole number. When you shift right, you remove the least significant bit (discarding its value), which is like dividing by 2 and rounding down.
Mini Quiz 🚀
- What is the result of shifting $01010101_2$ left by 3 bits?
- After shifting $11111111_2$ right by 4 bits, what is the new binary value?
- Explain why shifting left by 2 is the same as multiplying by 4.
Answers: 1️⃣ $01010101_2 \xrightarrow{\text{LSL 3}} 10101000_2$ (decimal 168). 2️⃣ $11111111_2 \xrightarrow{\text{LSR 4}} 00001111_2$ (decimal 15). 3️⃣ Each left shift doubles the number; shifting twice doubles twice → multiply by $2 \times 2 = 4$.
Key Takeaways
- Logical shifts move bits and fill with zeros.
- Left shift by n = multiply by $2^n$.
- Right shift by n = divide by $2^n$ (discard remainder).
- Shifts are fast operations used in low‑level programming and hardware design.
Revision
Log in to practice.