impera Open Impera

A levels Computer Science · Data representation

Number systems and binary arithmetic

1Number bases

BaseNameDigitsExample
2binary0, 10110 1011
10denary0–9107
16hexadecimal0–9, A–F (A = 10 ... F = 15)6B
Worked example

Converting 0110 1011

  1. Denary: 64 + 32 + 8 + 2 + 1 = 107.
  2. Hex: split into nibbles (groups of 4 bits): 0110 = 6, 1011 = 11 = B, so 6B.
  3. Check: 6 × 16 + 11 = 107.

Hexadecimal is used because it is shorter and easier for people to read than binary, and converts to binary easily (one hex digit = 4 bits). It is used for colour codes (#FF8800), memory addresses and MAC addresses.

Check yourself

What is hex 3E in denary?

  • 30

  • 314

  • 62

  • 53

Show the answer

62

3 × 16 + 14.

2Units of storage

PrefixSymbolValuePrefixSymbolValue
kilokB10³kibiKiB2¹⁰ = 1024
megaMB10⁶mebiMiB2²⁰
gigaGB10⁹gibiGiB2³⁰
teraTB10¹²tebiTiB2⁴⁰

A bit is one binary digit. A byte is 8 bits. With n bits, you can represent 2ⁿ different values.

Check yourself

How many different values can 10 bits represent?

  • 10

  • 100

  • 512

  • 1024

Show the answer

1024

2¹⁰.

3Binary addition and overflow

Rules: 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 0 carry 1, 1 + 1 + 1 = 1 carry 1.

Worked example

0110 1100 + 0011 1010

  1. Add column by column from the right, carrying as you go.
  2. Result: 1010 0110 = 166.
  3. Check: 108 + 58 = 166.

Overflow happens when a result is too big for the number of bits. In 8-bit unsigned binary the largest value is 255; in 8-bit two's complement it is 127, so 108 + 58 = 166 overflows if the numbers are signed (the result looks negative).

Check yourself

What is 1011 + 0110 in binary (unsigned)?

  • 10101

  • 1101

  • 10001

  • 1111

Show the answer

10001

11 + 6 = 17.

4Two's complement

Two's complement represents negative integers. The most significant bit has a negative place value.

−45 in 8-bit two's complement is 1101 0011. Only the most significant bit has a negative place value.

To find the two's complement of a number (to negate it): invert all the bits, then add 1.

Worked example

−45 in 8 bits

  1. 45 = 0010 1101.
  2. Invert: 1101 0010.
  3. Add 1: 1101 0011.
  4. Check: −128 + 64 + 16 + 2 + 1 = −45.

The range of n-bit two's complement is −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1: for 8 bits, −128 to 127.

Check yourself

What denary value is the 8-bit two's complement number 1111 1110?

  • −2

  • −126

  • 254

  • 2

Show the answer

−2

−128 + 126.

5Subtraction and shifts

To subtract, add the two's complement of the second number and ignore any carry out of the leftmost bit.

Worked example

50 − 20 in 8 bits

  1. 50 = 0011 0010. 20 = 0001 0100, so −20 = 1110 1100.
  2. 0011 0010 + 1110 1100 = 1 0001 1110.
  3. Ignore the carry out: 0001 1110 = 30.

Shifts move every bit:

  • A logical left shift by 1 multiplies an unsigned number by 2 (0000 0101 = 5 → 0000 1010 = 10).
  • A logical right shift by 1 divides by 2, losing the rightmost bit.
  • An arithmetic right shift copies the sign bit in, so it works for negative two's complement numbers.

Binary multiplication works like long multiplication: shift and add.

Check yourself

What is the result of a logical left shift by 2 on 0000 0011?

  • 0000 0110 (6)

  • 0000 0001 (1)

  • 0011 0000 (48)

  • 0000 1100 (12)

Show the answer

0000 1100 (12)

× 4.

6Fixed-point binary fractions

In fixed point, the binary point is in a fixed position. Bits after the point have values ½, ¼, ⅛, ...

Worked example

0110.1100

  1. Before the point: 0110 = 6.
  2. After the point: ½ + ¼ = 0.75.
  3. Value = 6.75.

Some fractions cannot be stored exactly: 0.1 in binary is 0.000110011... recurring. With 4 bits after the point, the closest value is 0.0010 = 0.125, or 0.0001 = 0.0625.

Fixed point is simple and fast, but its range is small for a given number of bits. Floating point solves this.

Check yourself

What is the fixed-point number 0011.0110 in denary?

  • 6.3

  • 3.75

  • 3.375

  • 3.6

Show the answer

3.375

3 + 0.25 + 0.125.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera