Data Representation

Data Representation

Logical shifts and two's complement

Number Systems Paper 1 13 min read

On this page
  1. In this note
  2. What you should already know
  3. Logical shifts
  4. Two's complement: negative numbers
  5. Summary
  6. Ways they test it
  7. What the examiner looks for
  8. Past-paper questions, solved
  9. Common mistakes
  10. Practice
  11. Can I do this?
  12. Quiz

In this note

Syllabus 1.1 Number systems
  • 1.1.5Perform logical left and right shifts (one or several places) on a positive 8-bit binary integer.
  • 1.1.5Explain the effect of a shift: multiply or divide by 2, 4, 8 …, and what happens to bits that fall off.
  • 1.1.6Use two's complement to represent positive and negative 8-bit integers, in both directions.

Before this: binary addition and overflow. Next: Text, sound and images.

Part 1Learn the topic

The notes: every idea from the start, with our own examples.

What you should already know

8-bit place values

128 64 32 16 8 4 2 1. The leftmost bit is the most significant bit (MSB, worth the most); the rightmost is the least significant bit (LSB, worth 1).

Quick check: what is the MSB of 0100 0001 worth if it is 1?

128.

Multiplying by 10 in denary

Moving every digit one place left multiplies a denary number by 10: 36 → 360. In binary, one place left multiplies by 2.

Quick check: what does moving 2 places left do in denary?

Multiplies by 100.

Logical shifts

A logical shift moves every bit in a register a set number of places to the left or to the right. The register stays 8 bits wide, so:

  • bits that move off the end of the register are lost;
  • the empty places at the other end are filled with 0s.

Logical left shift

Logical left shift of 2 places: 0001 0110 (22) becomes 0101 1000 (88). Two 0s come in on the right.
1286432168421
Before00010110
After01011000

General methodLogical left shift by n places

  1. Move every bit n places to the left.
  2. The n bits at the left end (the most significant bits) are lost.
  3. Fill the n empty places on the right with 0s.
  4. Effect: the number is multiplied by 2ⁿ (1 place × 2, 2 places × 4, 3 places × 8), as long as no 1s were lost.

Logical right shift

Logical right shift of 2 places: 1011 0100 (180) becomes 0010 1101 (45). Two 0s come in on the left.
1286432168421
Before10110100
After00101101

General methodLogical right shift by n places

  1. Move every bit n places to the right.
  2. The n bits at the right end (the least significant bits) are lost.
  3. Fill the n empty places on the left with 0s.
  4. Effect: the number is divided by 2ⁿ (1 place ÷ 2, 2 places ÷ 4, 4 places ÷ 16).

When bits that are 1 fall off

If a 1 is pushed off the end, the answer is no longer exactly × or ÷ 2ⁿ: the value becomes incorrect / inaccurate.

ShiftBeforeAfterWhat happened
right 10000 1101 (13)0000 0110 (6)13 ÷ 2 = 6.5, but the 1 on the right was lost: only the whole number 6 is left
left 11100 0011 (195)1000 0110 (134)195 × 2 = 390 needs 9 bits; the MSB 1 was lost, so the result is wrong

Try it yourself

1
Perform a logical left shift of 3 places on 0000 1101.
Show working
  1. 13 × 8 = 104 = 0110 1000

Answer 0110 1000

2
Perform a logical right shift of 3 places on 0101 1000.
Show working
  1. 88 ÷ 8 = 11

Answer 0000 1011

3
What is the effect of a logical right shift of 4 places on a positive binary integer?
Show answer

Answer It is divided by 16 (2⁴)

Two's complement: negative numbers

Ordinary binary can only hold positive numbers. Two's complement lets one 8-bit register hold positive and negative numbers. The trick: the most significant bit is worth −128 instead of +128.

1101 0011 = −128 + 64 + 16 + 2 + 1 = −45
−1286432168421
−4511010011

The sign bit

MSB 0 → the number is positive (or 0). MSB 1 → the number is negative.

Positive numbers

Exactly the same as ordinary binary, with a 0 at the front: 45 = 0010 1101.

Range

8-bit two's complement holds −128 to +127: 1000 0000 = −128 and 0111 1111 = +127.

−1

1111 1111 = −128 + 127 = −1. All 1s is −1, not −255.

Denary to two's complement

Method A: flip and add 1

General methodFlip and add 1

  1. Write the positive number in 8-bit binary.
  2. If the number is positive, stop: that is the answer.
  3. If negative, flip every bit (0 → 1, 1 → 0).
  4. Add 1 to the result.

Example Convert −45 to 8-bit two's complement.

Write 45, flip every bit, add 1.

  1. 45 = 0010 1101
  2. flip: 1101 0010
  3. add 1: 1101 0011

Answer 1101 0011

Method B: start from −128

General methodStart from −128

  1. Put a 1 in the −128 column.
  2. Work out what must be added to −128 to reach the number: −45 − (−128) = 83.
  3. Write 83 in the other seven columns: 64 + 16 + 2 + 1.

Example Convert −45 to 8-bit two's complement.

−128 + 83 = −45.

  1. 83 = 101 0011 in the 7 columns

Answer 1101 0011

Two's complement to denary

General methodTwo's complement to denary

  1. Look at the MSB. If it is 0, convert as ordinary binary.
  2. If it is 1, the number is negative: add the place values with the MSB worth −128.
  3. (Or: flip every bit, add 1, convert, and put a minus sign in front.)

Example Convert the two's complement integer 1110 1100 to denary.

MSB is 1, so the first column is −128.

  1. −128 + 64 + 32 + 8 + 4

Answer −20

Try it yourself

1
Convert −78 to an 8-bit two's complement integer.
Show working
  1. 78 = 0100 1110
  2. flip: 1011 0001
  3. +1: 1011 0010

Answer 1011 0010

2
Convert the two's complement integer 1011 0111 to denary.
Show working
  1. −128 + 32 + 16 + 4 + 2 + 1 = −73

Answer −73

3
Convert 45 to an 8-bit two's complement integer.
Show working
  1. positive: ordinary binary with 0 at the front

Answer 0010 1101

Summary

  • Logical shift: every bit moves left or right; bits that fall off are lost; 0s fill the empty places.
  • Left shift n places = × 2ⁿ; right shift n places = ÷ 2ⁿ. If 1s are lost, the result is inaccurate / wrong.
  • Two's complement: the MSB is worth −128. MSB 1 = negative. Range −128 to +127.
  • Negative denary → two's complement: write the positive in binary, flip every bit, add 1.
  • Two's complement → denary: −128 + the other place values (when the MSB is 1).

If you see …

  • "state the effect" of a shift× or ÷ by 2ⁿ, and bits lost → may be inaccurate
  • "describe the process" of a shiftbits move, MSB / LSB lost, 0s added at the other end
  • a negative number to storetwo's complement: flip and add 1, and show it
Part 2How it is examined

Question types, what the examiner gives marks for, and real past-paper questions solved.

Ways they test it

Type 1 Do the shift

"Give the 8-bit binary number stored after a logical left shift of three places" [1]

Spot it a register and a number of places.

  1. Copy the 8 bits, move them, fill with 0s, count that you still have 8 bits.

Type 2 Effect of the shift

"State one effect this logical shift has on the binary number" [1] / "State the mathematical effect of a right shift of four places" [2]

Spot it "effect".

  1. Multiplied / divided by 2ⁿ (give the number: by 8, by 16).
  2. Bits are lost, so the value may become incorrect.

Type 3 Describe the process

"Describe the process of the logical left shift" [2]

Spot it "describe" + "process".

  1. Every bit is moved left (by the number of places).
  2. The leftmost / most significant bit is lost.
  3. A 0 is added as the rightmost / least significant bit.

Type 4 Convert to or from two's complement

"Complete the register for −78. Show all your working" [2]

Spot it a negative denary number or a binary integer starting with 1.

  1. 1 mark for a working method (flip and add 1, or −128 + …).
  2. 1 mark for the correct answer.

Type 5 Name it

"Give the name of the number system used to represent negative binary integers" [1]

Spot it "negative".

  1. Two's complement.

What the examiner looks for

"Describe the process of a logical left shift" [2] + "State the effect" [1]

Marking points (one mark each, up to the marks shown)

  • Each / every bit in the binary number is shifted / moved to the left
  • The leftmost / most significant bit is lost
  • A 0 is added as the rightmost / least significant bit
  • Effect: the binary integer is multiplied by 2 (per place)

Keywords moved left most significant bit lost 0 added multiplied by 2

Two's complement conversion [2]

Marking points (one mark each, up to the marks shown)

  • One mark for a full method of working, e.g. conversion to binary then flipping and adding 1
  • One mark for the correct 8-bit answer

Keywords flip add 1 −128

Too vague an answer with no working (loses the method mark); fewer than 8 bits

The effect of a right shift

2210/12/M/J/23 Q2Paper 1 · Computer systems1 mark
State one effect this logical right shift (of three places) has on the binary number.
Credited
  • It is divided by 8
  • The value becomes incorrect / inaccurate because the rightmost bits are lost
Not credited
  • It gets smaller
  • It is divided by 3
Why

Give the exact operation: 3 places = ÷ 2³ = ÷ 8. "Smaller" is not specific enough.

From: 2210/12/M/J/23 Q2(b)(ii) mark scheme

Past-paper questions, solved

2210/12/O/N/23 Q2Paper 1 · Computer systems3 marksFull size
Left shift of two places on 11100011; store −99 in two's complement

How to start For (c) move every bit two places left and add two 0s. For (d) write 99 in binary first, then flip and add 1.

Show a full-mark answer and the mark scheme

(c) 1110 0011 → 1000 1100 (the two 1s on the left are lost).

(d) 99 = 0110 0011 → flip: 1001 1100 → add 1: 1001 1101. Check: −128 + 16 + 8 + 4 + 1 = −99.

PartAnswer / workingMarksGuidance
2(c)1 0 0 0 1 1 0 0B1
2(d)suitable working method, e.g. flip and add 1M1
2(d)1001 1101A1
Total: 3 marks
0478/12/F/M/24 Q8Paper 1 · Computer systems5 marksFull size
10110111 to denary; 45 as two's complement; the effect of a right shift of four places

How to start The MSB of 10110111 is 1, so it is negative: start from −128. 45 is positive, so it is plain binary. Four places right = ÷ 2⁴.

Show a full-mark answer and the mark scheme

(b)(i) −128 + 32 + 16 + 4 + 2 + 1 = −73 (or flip 1011 0111 → 0100 1000, add 1 → 0100 1001 = 73, so −73).

(b)(ii) 0010 1101

(c) The integer is divided by 16.

PartAnswer / workingMarksGuidance
8(b)(i)working, e.g. flip and add 1: 10110111 = 01001001M1
8(b)(i)−73A1
8(b)(ii)00101101B1
8(c)divide · … by 16 // 2⁴B2one mark each
Total: 5 marks
2210/12/O/N/24 Q3Paper 1 · Computer systems3 marksFull size
Describe a logical left shift and its effect

How to start "Describe the process" wants the three things that physically happen to the bits; "effect" wants the maths.

Show a full-mark answer and the mark scheme

(d)(i) Every bit in the integer is moved one place to the left. The leftmost (most significant) bit is lost and a 0 is added as the rightmost (least significant) bit.

(d)(ii) The integer is multiplied by 2.

PartAnswer / workingMarksGuidance
3(d)(i)Any two: every bit is shifted left · the leftmost / MSB is lost · a 0 is added as the rightmost / LSBB2
3(d)(ii)The binary integer is multiplied by 2B1
Total: 3 marks

Common mistakes

  • ✗ Shifting and ending up with 9 or 7 bits✓ Always exactly 8 bits after the shiftLost bits are gone; new places are 0s.
  • ✗ "Right shift of 3 divides by 3"✓ Divides by 2³ = 8Each place is a factor of 2.
  • ✗ Flipping the bits and forgetting to add 1✓ Flip, THEN add 1Flip alone gives one's complement, which is not on the syllabus.
  • ✗ Writing −73 as 1100 1001 (73 with a 1 in front)✓ 1011 0111Two's complement is not "sign and size".
  • ✗ No working for two's complement✓ Show the flip and the +1One of the two marks is for the method.

Practice

Practice: easy, medium, hard

Easy

1
Logical left shift of 1 place on 0010 0101.
Show answer

Answer 0100 1010

2
Logical right shift of 1 place on 0110 0100.
Show answer

Answer 0011 0010

3
Is the two's complement integer 1000 0101 positive or negative?
Show answer

Answer negative (MSB is 1)

Medium

1
Convert −25 to an 8-bit two's complement integer.
Show working
  1. 25 = 0001 1001
  2. flip 1110 0110
  3. +1 1110 0111

Answer 1110 0111

2
Convert the two's complement integer 1000 1110 to denary.
Show working
  1. −128 + 8 + 4 + 2

Answer −114

3
A logical right shift of 2 places is done on 1100 1011. Give the result and say why it is not exactly ÷ 4.
Show working
  1. 203 ÷ 4 = 50.75, the register holds 50

Answer 0011 0010: the two 1s on the right were lost

Hard

1
Convert −22 to an 8-bit two's complement integer. Show your working.
Show working
  1. 22 = 0001 0110
  2. flip 1110 1001
  3. +1 1110 1010

Answer 1110 1010

PartAnswer / workingMarksGuidance
flip and add 1 shownM1
1110 1010A1
Total: 2 marks
2
What are the smallest and largest numbers in 8-bit two's complement? Give each in binary.
Show answer

Answer −128 = 1000 0000; +127 = 0111 1111

3
A value 0001 0110 is shifted left 3 places. Give the result and its denary value. Has any data been lost?
Show answer

Answer 1011 0000 = 176; no 1s were lost (22 × 8 = 176)

Can I do this?

Anything unticked: revise that section first. All ticked: take the quiz.

Quiz

The multiple-choice and guided questions need JavaScript. The concept checks and exam practice below work without it.

Your progress on this note is saved when JavaScript is on.

⬇️ Download this chapter as a PDF: Full notes