Review lesson · 55 minutes

Signed numbers, then AND, OR and NOT

One byte can hold a negative number if we agree how to read it. This lesson shows three ways to do that, explains why computers use two’s complement, and then links flipping bits to the NOT gate.

We have covered signed binary, including eight-bit two’s complement, and the AND, OR and NOT gates. Use this page to review those topics.

What this lesson leaves out

This short bridge covers sign-magnitude, one’s complement and two’s complement only. BCD, Gray code, Excess-N, fixed-point and floating-point numbers are in a separate catch-up sequence.

Open the catch-up sequence →

By the end you can

  • Explain why the same eight bits can mean different numbers.
  • Write a negative number in eight-bit two’s complement by flipping the bits and adding one.
  • State the eight-bit two’s complement range and explain it.
  • Complete truth tables for AND, OR and NOT and recognise their symbols.
  • Tell Python’s Boolean not apart from bitwise inversion.

Textbook: Computer Science for the IB Diploma (Baumgarten, Ganea and Turland, 2025). Two’s complement is on printed page 47, and one’s complement is on page 48. The whole section runs from page 47 to page 51. Before the lesson, read only the part about two’s complement on page 47.

1 · One pattern, four meanings · 3 minutes

A byte is only eight bits. It does not say what kind of number it is. Before you can read it, you must know the encoding: the agreed rule for turning bits into a value.

The pattern 11111011 read with four encodings
EncodingHow to read itValue
UnsignedAdd the place values: 128 + 64 + 32 + 16 + 8 + 2 + 1251
Sign-magnitudeFirst bit is the sign (1 means negative). The other seven bits, 1111011, are 123−123
One’s complementFirst bit is 1, so flip every bit: 00000100 is 4−4
Two’s complementThe first bit is worth −128: −128 + 64 + 32 + 16 + 8 + 2 + 1−5

Unsigned means there is no sign, so every pattern is zero or positive. You used unsigned bytes in the earlier lessons on binary and hexadecimal.

Check: read 10000001 as an unsigned number and as a two’s complement number.

Show answer

Unsigned: 128 + 1 = 129. Two’s complement: −128 + 1 = −127.

2 · Three ways to write −5 · 5 minutes

Start from +5, which is 00000101 in every one of these encodings.

−5 in one byte
EncodingRule−5
Sign-magnitudeChange only the first bit to 110000101
One’s complementFlip every bit of +511111010
Two’s complementFlip every bit of +5, then add 111111011

Sign-magnitude and one’s complement are easy to read, but they have two problems. Each has two patterns for zero, and ordinary binary addition gives wrong answers. Two’s complement has one zero, and the same adding circuit works for positive and negative numbers.

Add 5 and −5 with ordinary binary addition
EncodingSumResult read in that encoding
Sign-magnitude00000101 + 10000101 = 10001010−10, which is wrong
One’s complement00000101 + 11111010 = 11111111−0, a second zero
Two’s complement00000101 + 11111011 = 1 000000000, after the ninth bit is dropped

Check: write the two patterns for zero in sign-magnitude.

Show answer

00000000 is +0 and 10000000 is −0.

3 · Flip the bits and add one · 5 minutes

To make a number negative in two’s complement, follow three steps.

  1. Write the positive number in eight bits. For 5 this is 00000101.
  2. Flip every bit: each 0 becomes 1 and each 1 becomes 0. This gives 11111010.
  3. Add 1. This gives 11111011, which is −5.

The same three steps take you back. Flip 11111011 to get 00000100, add 1 to get 00000101, which is 5.

Try it: use the byte below. Press “Load +5”, then “Flip all bits”, then “Add 1”. Watch the four readings change. Then choose your own number.

One byte, four readings

Press a bit to switch it between 0 and 1. Keyboard: Tab to a bit or button, then press Enter or Space.

Readings of the current pattern
EncodingValue
Pattern00000000
Unsigned0
Sign-magnitude0
One’s complement0
Two’s complement0

Check: write −12 in eight-bit two’s complement.

Show answer

12 is 00001100. Flip: 11110011. Add 1: 11110100.

Check: what number is 11110000 in two’s complement?

Show answer

−128 + 64 + 32 + 16 = −16. You can also flip and add one: 00001111 + 1 = 00010000 = 16, so the number is −16.

4 · The range: −128 to 127 · 4 minutes

Eight bits always give 256 different patterns, because 2 to the power 8 is 256. The encoding only decides which numbers those patterns stand for.

What 256 patterns are used for
EncodingSmallestLargestDifferent values
Unsigned0255256
Sign-magnitude−127127255, because zero appears twice
One’s complement−127127255, because zero appears twice
Two’s complement−128127256

In two’s complement, half of the patterns start with 1 and are negative: that is 128 numbers, from −128 to −1. The other half start with 0. One of those is zero, which leaves 127 positive numbers. This is why there is a −128 but no +128.

  • 01111111 is 127, the largest value.
  • 10000000 is −128, the smallest value.
  • 11111111 is −1.

Check: in eight-bit two’s complement, what happens when you add 1 to 127?

Show answer

01111111 + 1 = 10000000, which reads as −128. The true answer, 128, does not fit in the range. This is called overflow.

5 · Flipping a bit is a NOT gate · 2 minutes

In step 2 of “flip and add one” you changed every 0 to 1 and every 1 to 0. A NOT gate does exactly this to one bit. To flip a whole byte, a computer uses eight NOT gates, one for each bit.

So you have already used a logic gate. The rest of the lesson looks at NOT and two more gates.

6 · AND, OR and NOT · 20 minutes

A logic gate takes one or two inputs and gives one output. Every input and output is 0 or 1. A truth table lists every possible input and the output for each.

The three gates
GateRuleEveryday example
ANDOutput is 1 only when both inputs are 1A microwave runs only when the door is closed AND start is pressed
OROutput is 1 when at least one input is 1A doorbell rings when the front button OR the back button is pressed
NOTOutput is the opposite of the inputA fridge light is on when the door is NOT closed

Try it: choose a gate, predict the output for each row of its truth table, then switch the inputs to check. The table marks the row you are on.

Gate lab

Choose a gate, then press an input to switch it between 0 and 1. Keyboard: Tab to a control, then press Enter or Space.

Gate
Gate symbol with the current inputs and output A = 0B = 0Q = 0

Output 0

Truth table for the chosen gate
ABQcurrent row
Truth tables to copy
ABA AND BA OR BNOT A
00001
01011
10010
11110

Gates can also work on whole bytes, one column at a time. For example, 1100 AND 1010 is 1000, 1100 OR 1010 is 1110, and NOT 1100 is 0011.

7 · Python: not is different from ~ · 10 minutes

Python has two families of operators that look similar. Keep them apart.

Boolean and bitwise operators
IdeaBoolean: True or FalseBitwise: every bit of an integer
ANDand&
ORor|
NOTnot~
  • not 5 is False. Python treats any non-zero number as true, and not gives the opposite truth value.
  • ~5 is −6. The ~ operator flips every bit. That is the “flip” step only, without the “add one”, so the result is −5 − 1.
  • Python integers have no fixed width. To see the result as one byte, keep the lowest eight bits with & 0xFF.
print(not 5)                          # False
print(~5)                             # -6
print(format(~5 & 0xFF, "08b"))       # 11111010  flip only
print(format((~5 + 1) & 0xFF, "08b")) # 11111011  flip and add one: -5

Problem 1. Complete to_twos(value) so that it returns the eight-bit two’s complement pattern as text. Test 5, −5, 127, −128 and −1.

Hint

Use value & 0xFF to keep eight bits, then format(number, "08b") to show them with leading zeros.

Problem 2. Complete from_twos(bits) so that it returns the signed value of a pattern such as 11111011.

Hint

int(bits, 2) reads the pattern as unsigned. If the first character is 1, the true value is 256 less.

Open the two’s complement starter in the Python playground →

Problem 3. Run the gate starter. Before you run it, predict the four lines printed in part 3.

Open the Boolean and bitwise starter in the Python playground →

Answer to Problem 1

Show answer

return format(value & 0xFF, "08b"). The five patterns are 00000101, 11111011, 01111111, 10000000 and 11111111.

Answer to Problem 2

Show answer

After number = int(bits, 2), add: if bits[0] == "1": number = number − 256. Then from_twos("11111011") returns −5.

Answer to Problem 3

Show answer

False, then −6, then 11111010, then 11111011.

8 · Practice questions

Work on paper first. Open an answer only after you have written your own.

1. Write −20 in eight-bit two’s complement.

Show answer

20 is 00010100. Flip: 11101011. Add 1: 11101100.

2. The byte 10010110 is stored in memory. Give its value as unsigned and as two’s complement.

Show answer

Unsigned: 128 + 16 + 4 + 2 = 150. Two’s complement: 150 − 256 = −106.

3. Why does eight-bit two’s complement reach −128 but stop at +127?

Show answer

There are 256 patterns. The 128 that start with 1 are the negative numbers −128 to −1. The 128 that start with 0 must also include zero, so only 127 are left for positive numbers.

4. An alarm sounds only when the system is armed and a sensor is triggered. Which gate is this? What is the output when the system is armed but no sensor is triggered?

Show answer

An AND gate. The inputs are 1 and 0, so the output is 0 and the alarm is silent.

5. Work out NOT 10110010.

Show answer

01001101. Every bit is flipped.

6. Work out 10110010 AND 00001111, then 10110010 OR 00001111.

Show answer

AND gives 00000010. OR gives 10111111.

7. In Python, what do not 0 and ~0 give? Explain the difference.

Show answer

not 0 is True, because 0 counts as false and not gives the opposite truth value. ~0 is −1, because flipping every bit of 0 gives all ones, which is −1 in two’s complement.

Exit question

The pattern 11111011 is stored in one byte. What else must you know before you can say which number it is?

Answer

Show answer

The encoding. As unsigned it is 251, and as two’s complement it is −5. The bits alone do not tell you which one is meant.

What comes next

← All DP Computer Science lessons