AS Level · Topic 1.1
A-Level Computer Science 9618 / Topic 1 / AS

Negative numbers in binary.

A register has no minus sign, so negative integers need a representation of their own. One's complement and two's complement both use the leftmost bit for the sign, but only two's complement lets the processor subtract by simply adding. Learn both, then add, subtract and spot overflow.

The key idea

In two's complement the most significant bit has a negative place value: in 8 bits it is −128, so the range is −128 to +127. To negate a number, invert every bit (this alone gives the one's complement) and then add 1. Subtraction is done by adding the negative: A − B = A + (−B). Overflow occurs when the true result is outside the range: adding two numbers with the same sign gives a result with the opposite sign.

8-bit two's complement: the leftmost place value is negative −128643216 8421 1101 0011 −128 + 64 + 16 + 2 + 1 = −45 +45 = 00101101 invert: 11010010 add 1: 11010011 range in 8 bits: −128 to +127
Fig. 1: 11010011 in 8-bit two's complement is −45
Section 01

One's and two's complement.

In the lab's two's complement mode, set the bits and watch the leftmost one count as −128. Try to make the smallest and largest values you can.

One's complementTwo's complement
To negateInvert every bitInvert every bit, then add 1
−45 in 8 bits1101001011010011
8-bit range−127 to +127−128 to +127
ZeroTwo zeros: 00000000 and 11111111One zero: 00000000
SignLeftmost bit 0 means positive or zero; leftmost bit 1 means negative
Worked example Denary to two's complement
Write −45 in 8-bit two's complement.
  1. Write +45 in 8 bits: 00101101.
  2. Invert every bit (one's complement): 11010010.
  3. Add 1: 11010011.
  4. Check: −128 + 64 + 16 + 2 + 1 = −45.
Answer: 11010011
Worked example Two's complement to denary
What is 11100110 in 8-bit two's complement?
  1. The leftmost bit is worth −128.
  2. The other 1s are worth 64 + 32 + 4 + 2 = 102.
  3. −128 + 102 = −26.
Answer: −26
Your turn
Write −100 in 8-bit two's complement.
Examiner trap

Inverting the bits is not the whole job for two's complement. Forgetting to add 1 gives the one's complement, which is out by one.

Section 02

Subtract by adding.

Switch the lab to subtraction. The processor never subtracts directly: it finds the two's complement of the second number and adds.

Worked example Negative answer
Calculate 52 − 75 using 8-bit two's complement.
  1. 52 = 00110100 and 75 = 01001011.
  2. −75: invert to 10110100, add 1: 10110101.
  3. Add: 00110100 + 10110101 = 11101001.
  4. Check: −128 + 64 + 32 + 8 + 1 = −23, and 52 − 75 = −23.
Answer: 11101001 = −23
Worked example Positive answer
Calculate 75 − 52 using 8-bit two's complement.
  1. −52: 52 = 00110100, invert to 11001011, add 1: 11001100.
  2. Add: 01001011 + 11001100 = 1 00010111.
  3. Ignore the carry out of the leftmost bit: 00010111 = 16 + 4 + 2 + 1 = 23.
Answer: 00010111 = 23
Your turn
Calculate 38 − 91 in 8-bit two's complement.
Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

What is the range of an 8-bit two's complement integer?

Quick check+10 XP

In the lab, what value is 11111111 in two's complement?

Quick check+10 XP

What is the one's complement of 00001111?

Quick check+10 XP

How does a processor normally carry out A − B?

Section 03

When the result will not fit.

Overflow happens when a calculation produces a value the register cannot represent. In the lab, push an unsigned 8-bit sum past 255 and watch the ninth bit fall off the register. In two's complement the same problem shows up as an answer with the wrong sign.

Examiner trap

A carry out of the leftmost bit is not the test for overflow in two's complement. Check the signs: overflow only when two positives give a negative or two negatives give a positive.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

What is −77 in 8-bit two's complement?

Exam style+20 XP

Which 8-bit two's complement addition causes an overflow?

Exam style+20 XP

What is the range of a 12-bit two's complement integer?

Exam style+20 XP

The 8-bit pattern 10000000 is read in one's complement and then in two's complement. What are the two values?

Skill unlocked

Two's complement and binary arithmetic, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
One's and two's complement, converting negative integers, binary addition and subtraction, and overflow.