Perform a logical binary shift on a positive 8-bit binary integer and understand the effect

Data Representation – Logical Binary Shift (Cambridge IGCSE 0478)

Learning objectives

  • Convert between binary, decimal and hexadecimal for both positive and negative 8‑bit numbers.
  • Perform a logical left‑shift and a logical right‑shift on a positive 8‑bit binary integer.
  • Predict the effect of the shift on the decimal value.
  • Detect overflow when it occurs and explain why.
  • Distinguish a logical shift from an arithmetic shift and relate this to two’s‑complement representation.

Syllabus link

1.1 Number systems – Logical binary shift: “Perform a logical binary shift on a positive 8‑bit binary integer and understand the effect on the decimal value.”

1. Quick refresher – Number systems

Students must be fluent in converting between binary, decimal and hexadecimal. The table shows the first sixteen values; the exercises that follow require both directions of conversion.

DecimalBinary (4‑bit)Hexadecimal
000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F

Conversion drill (positive numbers)

  1. Convert 156₁₀ to binary and hexadecimal.
  2. Convert 0x9C (hex) back to decimal and binary.

Answers: 156₁₀ = 1001 1100₂ = 9C₁₆; 0x9C = 156₁₀ = 1001 1100₂.

2. Two’s‑complement reminder (signed 8‑bit integers)

  • Range: -128 … +127.
  • Positive numbers are represented exactly as their unsigned binary form.
  • To obtain a negative number: invert every bit (1→0, 0→1) and add 1.

Worked example – negative number

Find the 8‑bit two’s‑complement representation of -45:

  1. 45₁₀ = 0010 1101₂.
  2. Invert bits → 1101 0010₂.
  3. Add 1 → 1101 0011₂. Hence -45 = 1101 0011₂.

3. What is a logical shift?

A logical shift treats the 8‑bit word as **unsigned**. All bits are moved left (<<) or right (>>) by the required number of positions; the vacated positions are filled with 0. Any bits that move beyond the 8‑bit boundary are discarded (lost).

Bit positions are numbered from 7 (most‑significant bit, MSB) down to 0 (least‑significant bit, LSB).

4. Quick reference formulas (unsigned arithmetic)

Left‑shift by n bits:
Result = (value × 2ⁿ) mod 256

Right‑shift by n bits:
Result = ⌊ value ÷ 2ⁿ ⌋

5. Logical left‑shift (<<)

Each left‑shift multiplies the unsigned value by 2. If a 1 is shifted out of bit 7, that bit is lost – this is the overflow condition for unsigned numbers.

StepBinary before shiftBinary after 1‑bit left‑shiftDecimal value
10010 01110100 111039 × 2 = 78
20100 11101001 110078 × 2 = 156
3 (overflow)1001 11000011 1000Bits 8‑9 discarded → 56

6. Logical right‑shift (>>)

Each right‑shift divides the unsigned value by 2, discarding the LSB and inserting a 0 at the MSB.

StepBinary before shiftBinary after 1‑bit right‑shiftDecimal value
10010 01110001 0011⌊39/2⌋ = 19
20001 00110000 1001⌊19/2⌋ = 9
30000 10010000 0100⌊9/2⌋ = 4

7. Detecting overflow – checklist

  • Is a 1 shifted out of bit 7 during a left‑shift? → Overflow (value reduced modulo 256).
  • In signed two’s‑complement arithmetic, does the lost bit change the sign (positive becomes negative or vice‑versa)? → Signed overflow.
  • Right‑shifts never overflow for unsigned numbers because zeros are inserted on the left.

8. Logical shift vs. arithmetic shift

FeatureLogical shiftArithmetic shift
PurposeUsed for **unsigned** data.Used for **signed** two’s‑complement data.
Left‑shiftIdentical to arithmetic left‑shift (insert 0 on the right).Same as logical left‑shift.
Right‑shiftInsert 0 into the MSB.Copy the original MSB (sign bit) into the new MSB.
Effect on signSign is not preserved – a positive number may become negative after a right‑shift.Sign is preserved – the number stays positive or negative.

9. Step‑by‑step procedure for a logical shift

  1. Write the 8‑bit binary number, grouping bits for readability (e.g. 0010 0111).
  2. Choose the direction:
    • Left‑shift**: move each bit one position toward the MSB.
    • Right‑shift**: move each bit one position toward the LSB.
  3. Insert a 0 into the newly‑vacated position (MSB for right‑shift, LSB for left‑shift).
  4. Discard any bit that moves beyond the 8‑bit boundary – this is the overflow bit.
  5. Convert the resulting 8‑bit pattern back to decimal to verify the effect.
  6. Use the overflow checklist (Section 7) to decide whether overflow has occurred.

10. Worked examples

10.1 Left‑shift by two positions (unsigned)

  1. Decimal 45 → binary 0010 1101₂.
  2. First left‑shift: 0101 1010₂ = 90.
  3. Second left‑shift: 1011 0100₂ = 180.
  4. No overflow because no 1 left the MSB during the two shifts.

10.2 Right‑shift by two positions (unsigned)

  1. Binary 1100 1011₂ (= 203₁₀).
  2. First right‑shift: 0110 0101₂ = 101.
  3. Second right‑shift: 0011 0010₂ = 50.

10.3 Binary addition with overflow (unsigned)

Add 150₁₀ and 120₁₀.

  1. 150₁₀ = 1001 0110₂.
    120₁₀ = 0111 1000₂.
  2. Binary addition:
    1001 0110
    +0111 1000
    -----------
    1 0000 1110
  3. The 9‑th bit (carry out of the MSB) is discarded → result = 0000 1110₂ = 14₁₀.
  4. Since a 1 was shifted out of the MSB, overflow has occurred (the true sum 270 exceeds 255).

10.4 Why logical right‑shift does not preserve sign (signed example)

  • Signed value –30 → two’s‑complement 1110 0010₂ (MSB = 1, so negative).
  • Logical right‑shift by one: 0111 0001₂ = 113₁₀ (now positive!).
  • Arithmetic right‑shift would copy the sign bit: 1111 0001₂ = –15₁₀, preserving negativity.

11. Common mistakes

  • Using a logical right‑shift for a signed number and assuming the sign is kept.
  • Forgetting to insert a 0 in the vacated MSB/LSB.
  • Including the overflow bit in the final 8‑bit result.
  • Mixing up unsigned and signed contexts when applying the formulas.

12. Practice questions

  1. Logical left‑shift 0011 0100₂ by one position. Write the new binary and decimal values.
  2. Logical right‑shift 1100 1011₂ by two positions. Write the new binary and decimal values.
  3. Explain why logical right‑shifting 1000 0001₂ does not preserve the sign of a signed 8‑bit integer.
  4. Convert the following:
    • 156₁₀ → binary → hexadecimal.
    • 0x9C → binary → decimal.
  5. Add the unsigned numbers 150 and 120**. Show the binary addition, indicate any overflow, and give the final 8‑bit result in decimal.
  6. Calculate the size (in bytes) of an 8‑bit greyscale image that is 64 × 64 pixels. If each pixel value is left‑shifted by one position and the overflow bits are discarded, does the file size change? Explain.

13. Extension – linking to later syllabus topics

Logical shifts underpin many algorithms that appear later in the course:

  • Bit‑masking in image processing (e.g. extracting colour channels, changing colour depth).
  • Checksum and CRC calculations for data transmission.
  • Fast multiplication or division by powers of two in assembly‑language programs.

Mastering logical shifts therefore makes these advanced topics much easier.

14. Further reading

Next in the syllabus: Text, sound and images – representation of non‑numeric data (ASCII, Unicode, colour depth, sample rate). Review how binary patterns encode characters and pixel values, and notice where logical shifts are used to manipulate those patterns.

Create an account or Login to take a Quiz

41 views
0 improvement suggestions

Log in to suggest improvements to this note.