[1.1.5] Logical shifts (8‑bit)

Logical Binary Shifts

Computers need to perform mathematical operations quickly and efficiently. Whilst addition and multiplication are fundamental operations, there exists a remarkably fast way to multiply or divide binary numbers by powers of 2: logical binary shifts. A binary shift is an operation that moves all the bits in a binary number either left or right by a specified number of positions. Understanding how shifts work and their effects on values is essential for efficient computing and forms the basis of many optimization techniques used in software development.

Binary shifts are particularly important because they can be performed extremely quickly by computer hardware - much faster than traditional multiplication or division operations. Processors have dedicated circuits for shifting that complete the operation in a single clock cycle, making shifts one of the fastest operations a computer can perform. This efficiency explains why shifts appear in graphics processing, compression algorithms, network protocols, and countless other computing applications.

Understanding Logical Shift Left

How Logical Shift Left Works

A logical shift left (LSL) operation moves every bit in a binary number one position to the left. When bits shift left, the leftmost bit (the most significant bit) is lost or discarded, and a 0 is inserted at the rightmost position (the least significant bit). This operation is sometimes written as LSL or simply as a left shift with the symbol <<.

Think of a logical shift left as a conveyor belt moving left. Each bit moves one space to the left, the bit at the far left falls off the end, and a zero enters from the right. This simple mechanical process has a profound mathematical effect on the value represented by the binary number.

Effect on Value: Multiplication by 2

The remarkable property of logical shift left is that shifting a binary number left by one position multiplies its value by 2. This happens because in binary, each position represents a power of 2, and moving a bit one position left doubles the power of 2 it represents. For example, a bit in the 23 position (value 8) moves to the 24 position (value 16), effectively doubling its contribution to the total value.

Example: Shift 00000101 (5 in denary) left by one position

Position 7 6 5 4 3 2 1 0
Original 0 0 0 0 0 1 0 1
After LSL 1 0 0 0 0 1 0 1 0

Result: 00000101 (5) becomes 00001010 (10)

Effect: 5 × 2 = 10 ✓

Notice how each bit moved one position left (toward the more significant positions), the leftmost 0 was discarded, and a 0 was added on the right. The value doubled from 5 to 10, demonstrating the multiplicative effect of the left shift.

Multiple Left Shifts

Shifting left by multiple positions multiplies by higher powers of 2. Shifting left by n positions multiplies the value by 2n. Each additional shift left doubles the value again, so two shifts multiply by 4, three shifts multiply by 8, and so on.

Example: Shift 00000011 (3 in denary) left by three positions

Operation Binary Denary Value
Original 00000011 3
After LSL 1 00000110 6 (3 × 2)
After LSL 2 00001100 12 (3 × 4)
After LSL 3 00011000 24 (3 × 8)

Result: Shifting left by 3 positions multiplied the value by 23 = 8

Verification: 3 × 8 = 24 ✓

Understanding Logical Shift Right

How Logical Shift Right Works

A logical shift right (LSR) operation moves every bit in a binary number one position to the right. When bits shift right, the rightmost bit (the least significant bit) is lost or discarded, and a 0 is inserted at the leftmost position (the most significant bit). This operation is sometimes written as LSR or simply as a right shift with the symbol >>.

Using the conveyor belt analogy again, logical shift right moves the belt to the right. Each bit moves one space right, the bit at the far right falls off, and a zero enters from the left. This produces the opposite mathematical effect to a left shift.

Effect on Value: Division by 2

Logical shift right divides the value by 2, using integer division (which discards any remainder). This occurs because moving bits right halves the power of 2 each bit represents. A bit in the 24 position (value 16) moves to the 23 position (value 8), halving its contribution to the total value. Any fractional result is lost when the rightmost bit is discarded.

Example: Shift 00001010 (10 in denary) right by one position

Position 7 6 5 4 3 2 1 0
Original 0 0 0 0 1 0 1 0
After LSR 1 0 0 0 0 0 1 0 1

Result: 00001010 (10) becomes 00000101 (5)

Effect: 10 ÷ 2 = 5 ✓

Each bit moved one position right (toward the less significant positions), a 0 was inserted on the left, and the rightmost 0 was discarded. The value halved from 10 to 5.

Multiple Right Shifts and Integer Division

Shifting right by n positions divides by 2n, using integer division. When the result would be a fraction, the remainder is lost because the bits that would represent the fractional part are discarded. This is equivalent to performing floor division in mathematics.

Example: Shift 00001011 (11 in denary) right by two positions

Operation Binary Denary Value
Original 00001011 11
After LSR 1 00000101 5 (11 ÷ 2 = 5.5, rounded down to 5)
After LSR 2 00000010 2 (11 ÷ 4 = 2.75, rounded down to 2)

Result: Shifting right by 2 positions divided by 22 = 4

Verification: 11 ÷ 4 = 2.75, which becomes 2 with integer division ✓

Data lost: The fractional part (0.75) is permanently lost when the rightmost bits are discarded.

Data Loss in Binary Shifts

Bits Lost in Left Shifts

When performing a left shift, any 1 bits that move beyond the leftmost position are permanently lost. This is critical to understand because it means that left shifts do not always accurately multiply by 2 - if significant bits are lost, the result will be incorrect. This situation is similar to overflow in addition, where the result becomes too large to fit in the available bits.

Example of data loss in left shift:

Shift 11000000 (192 in denary) left by one position:

  • Original: 11000000 (192)
  • Expected result: 192 × 2 = 384
  • Actual 8-bit result: 10000000 (128)
  • Problem: The leftmost 1 bit was lost, making the result incorrect
  • Correct result requires 9 bits: 110000000 (384)

This data loss means that left shifts can only safely multiply values when the result fits within 8 bits (i.e. when the result is ≤ 255). Any value over 127 cannot be doubled without losing bits, and any value over 63 cannot be quadrupled without losing bits.

Bits Lost in Right Shifts

Right shifts also lose data, but in a different way. When shifting right, the rightmost bits are discarded, which means any information they contained is permanently lost. This is why right shifts perform integer division - the fractional part that would be represented by the lost bits simply disappears.

Example of data loss in right shift:

Shift 00000111 (7 in denary) right by one position:

  • Original: 00000111 (7)
  • After LSR 1: 00000011 (3)
  • Mathematical division: 7 ÷ 2 = 3.5
  • Lost information: The 0.5 fractional part (represented by the rightmost 1 bit) is discarded

This data loss is acceptable when integer division is desired, but it means that repeatedly shifting right and then left will not recover the original value if bits with 1s were lost. Right shifts are non-reversible operations when data loss occurs.

Practical Examples with 8-bit Numbers

Example 1: Successful Left Shift

Task: Shift 00010110 (22 in denary) left by two positions

Step Binary Denary Calculation
Original 00010110 22 -
After LSL 1 00101100 44 22 × 2 = 44
After LSL 2 01011000 88 22 × 4 = 88

Result: 01011000 (88)

Verification: 22 × 4 = 88 ✓ (No data loss because 88 < 256)

Example 2: Left Shift with Data Loss

Task: Shift 01100100 (100 in denary) left by two positions

Step Binary Denary Notes
Original 01100100 100 -
After LSL 1 11001000 200 100 × 2 = 200 ✓
After LSL 2 10010000 144 Expected 400, got 144 ✗ (bit lost!)

Result: 10010000 (144)

Expected: 100 × 4 = 400

Problem: The leftmost 1 bit was lost when shifting from 11001000 to 10010000, causing incorrect result. The value 400 requires 9 bits to represent (110010000), but only 8 bits are available.

Example 3: Right Shift with Integer Division

Task: Shift 01010101 (85 in denary) right by three positions

Step Binary Denary Calculation
Original 01010101 85 -
After LSR 1 00101010 42 85 ÷ 2 = 42.5 → 42
After LSR 2 00010101 21 85 ÷ 4 = 21.25 → 21
After LSR 3 00001010 10 85 ÷ 8 = 10.625 → 10

Result: 00001010 (10)

Verification: 85 ÷ 8 = 10.625, which rounds down to 10 ✓

Data lost: The fractional part 0.625 was discarded through the rightmost bits being lost.

Summary of Effects

Operation Effect on Value Bits Lost Constraint
LSL (left shift) by n Multiply by 2n Left side (most significant) Result must fit in 8 bits
LSR (right shift) by n Divide by 2n (integer division) Right side (least significant) Fractional part is lost

Remember that shifts are powerful tools when used correctly, but they come with important limitations regarding data loss. Always consider whether the mathematical operation you need can be accurately performed with the available bits, and understand that right shifts permanently discard fractional information.

 Key Takeaways

  • A logical shift left (LSL) moves all bits one position left, discards the leftmost bit, and inserts 0 on the right, effectively multiplying the value by 2
  • A logical shift right (LSR) moves all bits one position right, discards the rightmost bit, and inserts 0 on the left, effectively dividing the value by 2 using integer division
  • Shifting left by n positions multiplies by 2n, but only produces correct results if the answer fits within 8 bits (≤ 255)
  • Shifting right by n positions divides by 2n, but any fractional remainder is lost when rightmost bits are discarded
  • Binary shifts cause data loss: left shifts lose significant bits if the result exceeds 255, whilst right shifts lose fractional information
  • Binary shifts are extremely fast operations because they are implemented as simple wire rearrangements in hardware, making them much faster than traditional multiplication or division