Cambridge IGCSE Computer Science

Chapter 1 · Data representation

1.1 Number systems

Computers use binary internally, but Computer Science students must be able to move confidently between binary, denary and hexadecimal, understand where hexadecimal is used, add binary values, recognise overflow, perform logical shifts and represent signed integers using two’s complement.

Binary · denary · hexConversionsBinary arithmeticTwo’s complement

What you need to know

This page covers the complete Topic 1.1 sequence from the supplied course book. The explanations are rewritten in simpler language, but the scope remains the same: how binary represents data; the three number systems and conversions; uses of hexadecimal; addition and overflow; logical shifts; and two’s complement for positive and negative integers.

Jump to a subtopic

1.1.1

Binary represents data

Whatever kind of data a computer is working with—numbers, text, images, sound or instructions—it must ultimately be represented in a form the computer’s electronic circuits can store and process. That form is binary.

Binary uses only two digits: 0 and 1. This matches the two-state nature of the tiny electronic switches inside digital systems. A simplified model is:

1 → ON

A switch or electronic state is active/on.

0 → OFF

A switch or electronic state is inactive/off.

These two states can be combined into very long patterns of bits. The patterns can represent values and instructions, and the computer can use logic gates to store and process them.

Key idea: 0 and 1 are not merely “small numbers” here. They are symbols for two physical/electronic states, which is why binary is such a natural system for digital computers.
Check that idea before moving on.
1.1.2

Binary, denary and hexadecimal systems

Denary (base 10)

Denary is the everyday number system. It has ten digits, 0–9, and each position is a power of 10. For example, in 25,177 the columns represent 10,000s, 1,000s, 100s, 10s and units.

10⁴ · 10³ · 10² · 10¹ · 10⁰ → 10000 · 1000 · 100 · 10 · 1

Binary (base 2)

Binary has only two digits, 0 and 1. Each position is a power of 2. An 8-bit binary value uses the place values shown below.

Power2⁷2⁶2⁵2⁴2⁰
Value1286432168421

Binary → denary

Add the place values wherever there is a 1. For example:

Example

10110110

128 + 32 + 16 + 4 + 2 = 182.

The same method works for 8-bit, 12-bit, 16-bit or larger values; you simply continue the powers of two.

Denary → binary: two methods

Method 1 — powers of two

Starting with the largest useful power of two, subtract it from the denary value. Put 1 in a column you use and 0 in a column you do not use.

Method 2 — repeated division by 2

Divide by 2 repeatedly, recording every remainder. When the quotient reaches 0, read the remainders from bottom to top.

Original Figure 1.1 showing repeated division by 2 to convert 142 to binary
Original Figure 1.1 from the supplied course book: repeated division by 2 and reading the remainders upwards.
Original Figure 1.1c showing conversion of 35000 to a 16-bit binary value
Original Figure 1.1c: the same method also works for a larger 16-bit value.

Hexadecimal (base 16)

Hexadecimal, often shortened to hex, needs sixteen symbols. It uses 0–9 followed by A–F for denary values 10–15. Because 16 = 2⁴, one hexadecimal digit corresponds exactly to four binary bits.

BinaryHexDenaryBinaryHexDenary
000000100088
000111100199
0010221010A10
0011331011B11
0100441100C12
0101551101D13
0110661110E14
0111771111F15

Binary ↔ hexadecimal

Binary to hex: start at the right, divide the bits into groups of four, and pad the left-most group with leading zeros if necessary. Convert each group using the table above.

Example

1011 1110 0001 → B E 1 → BE1

Hex to binary: replace every hex digit with its four-bit equivalent, then join the groups together.

Hexadecimal ↔ denary

To convert hex to denary, use powers of 16. For a three-digit hex number the place values are 256, 16 and 1. Remember A–F mean 10–15.

Example: 4A5₁₆

4×256 + 10×16 + 5 = 1024 + 160 + 5 = 1189.

To convert denary to hex, repeatedly divide by 16, record each remainder, convert remainders 10–15 to A–F, then read the remainders from bottom to top.

Original Figure 1.2a showing repeated division by 16
Original Figure 1.2a: denary to hexadecimal by repeated division by 16.
Original Figure 1.2b showing a larger repeated division by 16 example
Original Figure 1.2b: a larger example of the same conversion method.
Exam habit: show the grouping or place values in conversion questions. A correct answer with visible working makes your method much easier to follow.
Practise conversions in both directions.
1.1.3

Use of the hexadecimal system

Computers still work internally with binary. Hexadecimal is useful mainly because it gives people a shorter, more readable way to write binary values: one hex digit replaces four binary bits. For example, a 16-bit binary pattern can be written using only four hex digits.

Error codes

System error codes are often displayed in hexadecimal. The code can identify an error or the memory/location associated with it, helping programmers and technicians interpret what went wrong.

MAC addresses

A MAC address identifies a network interface. A common 48-bit MAC address is displayed as six groups of two hexadecimal digits, for example 00:1C:B3:4F:25:FE. The first half identifies the manufacturer and the second half identifies the device/interface serial part. 64-bit forms also exist.

IP addresses

IPv4 is a 32-bit address and is commonly shown in denary dotted notation. IPv6 uses 128 bits and is commonly written as hexadecimal groups separated by colons, making a very long binary address manageable for people.

HTML colour codes

Web colours can be written as #RRGGBB. Each pair ranges from 00 to FF, so red, green and blue each have 256 possible intensity values.

Original Figure 1.3 showing hexadecimal error codes
Original Figure 1.3 from the supplied course book: examples of hexadecimal error codes.

HTML colour codes in more detail

The six digits after the # symbol represent red, green and blue, two hex digits each. Examples:

CodeMeaning
#FF0000maximum red, no green, no blue
#00FF00maximum green
#0000FFmaximum blue
#FF00FFred + blue → fuchsia
#FF8000orange

There are 256 possible red values × 256 green values × 256 blue values = 16,777,216 possible RGB colours.

Original Figure 1.5 showing RGB hexadecimal colour intensity
Original Figure 1.5: how the three pairs of hexadecimal digits correspond to red, green and blue intensity.
Check the four uses of hexadecimal.
1.1.4

Addition of binary numbers

Binary addition follows the same column-by-column idea as denary addition, but because binary has only 0 and 1, you carry whenever the total for a column is greater than 1.

AdditionCarryWrite as sum bit
0 + 000
0 + 101
1 + 001
1 + 110
1 + 1 + 111

Example

Add 00110101 and 01000110.

00110101
+01000110
────────
01111011

The result is 123 denary, matching 53 + 70.

Overflow

An unsigned 8-bit register can store values from 0 to 255. If an addition needs a ninth bit, the true answer is too large for the 8-bit register. This is an overflow error.

Original course-book example showing binary overflow creating a ninth bit
Original course-book overflow example: a ninth result bit shows that an 8-bit register is not large enough.
Maximum unsigned value with n bits = 2ⁿ − 1
8 bits → 255 · 16 bits → 65,535 · 32 bits → 4,294,967,295
Do not simply discard a ninth bit and treat the remaining eight bits as the correct answer. Discarding it changes the value; the ninth bit is the evidence that overflow has occurred.
Practise carries and overflow.
1.1.5

Logical binary shifts

A logical shift moves every bit left or right by a stated number of positions. Vacated bit positions are filled with 0.

Shift left

When no significant 1-bit is lost, each one-place left shift multiplies the value by 2. Two places multiply by 2², three places by 2³, and so on.

Shift right

When no significant 1-bit is lost, each one-place right shift divides the value by 2. Two places divide by 2², and so on.

The left-most bit is the most significant bit (MSB); the right-most bit is the least significant bit (LSB).

Original course-book logical shift diagrams showing left and right shifts in an 8-bit register
Original course-book visual: logical shifts, zero-fill positions and the loss of bits at the edge of an 8-bit register.
Fixed-size registers matter. If a 1 is shifted out of the left or right edge, information is lost. At that point the simple “multiply by 2” or “divide by 2” interpretation can produce an incorrect arithmetic result.

Example

00010110 represents 22.

Shift left twice → 01011000 = 88, which is 22 × 4.

Shift right once from the original → 00001011 = 11.

Practise shifts and spotting lost bits.
1.1.6

Two’s complement (binary numbers)

So far, ordinary 8-bit examples have treated all values as positive. Two’s complement provides a standard way to use the same fixed number of bits to represent both positive and negative integers.

8-bit place values

Bit positionMSBLSB
Two’s-complement weight−1286432168421

The left-most bit now has a negative weight. In 8 bits the range is −128 to +127. A leading 0 indicates a non-negative value; a leading 1 indicates a negative value.

General range: for an n-bit two’s-complement integer, the range is −2n−1 to 2n−1 − 1.

Positive values

Positive values are written in the same way as ordinary binary, but the most significant bit must be 0. For example, +38 is 00100110.

Two’s-complement binary → denary

Use the place values, including the negative left-most weight. For example:

Example: 11101011₂

−128 + 64 + 32 + 8 + 2 + 1 = −21.

Negative denary → two’s-complement binary

Method 1 — use the negative place value

Start with −128 in an 8-bit number, then use positive place values to reach the required negative total.

Method 2 — invert and add 1

  1. Write the positive value in the required number of bits.
  2. Invert every bit (0↔1).
  3. Add 1.

Example: −37 in 8 bits

+37 = 00100101

Invert → 11011010

Add 1 → 11011011

The same principle works for other bit widths. With 4 bits the range is −8 to +7; with 12 bits it is −2048 to +2047. The course-book example demonstrates this with a 12-bit value as well as 8-bit values.

Quick sign check: if an 8-bit two’s-complement number begins with 1, your denary answer must be negative. If it begins with 0, it is non-negative.
Practise signed binary.

Topic 1.1 revision checklist

Explain why computers represent data in binary.
Convert between binary and denary.
Convert between binary and hexadecimal.
Convert between hexadecimal and denary.
Explain practical uses of hexadecimal.
Add positive binary values and recognise overflow.
Perform and interpret logical shifts.
Convert positive and negative values using two’s complement.
Ready for a mixed check?
← Computer Science contentsNext: 1.2 Text, sound and images →