EveryoneWork through this section fully. Binary and hexadecimal appear throughout the A Level: in data representation, networking, assembly language, and beyond. The practice questions at the end are worth doing properly.

Why binary?

A computer is made from billions of tiny switches called transistors. Each switch has two states: on or off. That is it. There is no "a bit on" or "mostly off". Just two states.

Because there are only two states, computers use a number system with only two digits: 0 and 1. A 0 represents a switch that is off. A 1 represents a switch that is on. This is binary.

Every piece of data a computer handles, whether it is a number, a letter, an image, a sound file or a video, is ultimately stored as a sequence of 0s and 1s. Understanding how that works is fundamental to everything else in this course.

New to CSIt might seem strange that something as capable as a modern computer is built on just two values. But consider: a light switch is also just on or off, and by wiring millions of them together in clever arrangements, you can do extraordinary things. Scale matters enormously in computing.

Denary (base 10)

The number system you use every day is denary, also called base 10. It uses ten digits: 0 through 9. The position of each digit tells you its value.

Take the number 347. It means:

100s
3
10s
4
1s
7

3 × 100 + 4 × 10 + 7 × 1 = 347. Each column is ten times the value of the column to its right. That is what "base 10" means.

Binary (base 2)

Binary works exactly the same way, but each column is two times the value of the column to its right, and the only digits available are 0 and 1.

Here is an 8-bit binary number: 10110101

128
1
64
0
32
1
16
1
8
0
4
1
2
0
1
1

To convert to denary, add up the column values where there is a 1:

128 + 32 + 16 + 4 + 1 = 181

Converting denary to binary

The most reliable method is repeated division by 2. Divide your number by 2, note the remainder, repeat until you reach 0. Read the remainders from bottom to top.

New to CSAn alternative method that many students find more intuitive: write out the column headers (128, 64, 32, 16, 8, 4, 2, 1) and work left to right. Ask "can I subtract this column value from what I have left?" If yes, write a 1 and subtract it. If no, write a 0 and move on.
Worked example: convert 93 to binary
1 128 > 93, so write 0. Move to 64. 0_______
2 64 ≤ 93. Write 1. 93 - 64 = 29 remaining. 01______
3 32 > 29, so write 0. 010_____
4 16 ≤ 29. Write 1. 29 - 16 = 13 remaining. 0101____
5 8 ≤ 13. Write 1. 13 - 8 = 5 remaining. 01011___
6 4 ≤ 5. Write 1. 5 - 4 = 1 remaining. 010111__
7 2 > 1, so write 0. 0101110_
8 1 ≤ 1. Write 1. 1 - 1 = 0. Done. 01011101

Check: 64 + 16 + 8 + 4 + 1 = 93. Correct.

Binary addition

Adding binary numbers works just like adding denary, but the only digits are 0 and 1. The carry rules are simple:

ABSumCarry
0000
0110
1010
1101
11 + carry 111
Example: 01101100 + 00110101
  1 1 1  1 1
  0 1 1 0 1 1 0 0
+ 0 0 1 1 0 1 0 1
  1 0 1 0 0 0 0 1

Check: 108 + 53 = 161. And 10100001 in binary is 128 + 32 + 1 = 161. Correct.

OverflowIf the result of a binary addition needs more bits than are available, you get an overflow error. For example, adding two 8-bit numbers that produce a 9-bit result. The 9th bit is lost and the stored result is wrong. This is a real source of bugs in software.
ExtensionBinary multiplication works by repeated shifting and addition, and division by repeated shifting and subtraction. These are worth exploring if you are interested. They also reveal why powers of 2 appear so frequently in computing: multiplying by 2 in binary is just a left shift by one position, which is extremely fast for a processor.

Negative numbers: two's complement

So far we have only looked at positive numbers. But computers need to store negative numbers too. The standard method is called two's complement.

In two's complement, the leftmost bit is the sign bit. If it is 0, the number is positive. If it is 1, the number is negative. But the negative value is not simply applied: the whole system shifts so that the column header for the leftmost bit becomes negative.

For an 8-bit two's complement number, the column headers become:

-128
1
64
0
32
0
16
1
8
1
4
1
2
1
1
1

-128 + 16 + 8 + 4 + 2 + 1 = -97

Converting a positive number to its negative two's complement

Worked example: represent -46 in 8-bit two's complement
1 Write +46 in binary. 00101110
2 Flip all the bits (0 becomes 1, 1 becomes 0). 11010001
3 Add 1. 11010010

Check: -128 + 64 + 16 + 2 = -46. Correct.

The range of an 8-bit two's complement number is -128 to +127. Notice it is not symmetrical: there is one more negative value than positive. This is a property of two's complement that comes up in exam questions.

ExtensionOne of the elegant properties of two's complement is that addition works identically for positive and negative numbers. The processor does not need a separate subtraction circuit: it just negates and adds. Try adding -46 and +46 in two's complement and verify you get 00000000 (with an overflow carry that is discarded).

Hexadecimal (base 16)

Binary is precise but hard to read. The number 11111010001011000 is difficult to scan, easy to miscount, and painful to type. Computers and programmers needed a more compact notation.

Hexadecimal is base 16. It uses 16 digits: 0-9 and then A-F (where A=10, B=11, C=12, D=13, E=14, F=15). One hex digit represents exactly four binary digits (a nibble), which makes conversion between the two trivial.

DenaryBinaryHex
000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F

Binary to hex

Group the binary digits into nibbles (groups of 4) from the right, then convert each nibble using the table above.

Worked example: convert 11111010001011000 to hex
1 Pad to a multiple of 4 bits from the left. 0001 1111 0100 0101 1000
2 Convert each nibble: 0001=1, 1111=F, 0100=4, 0101=5, 1000=8 1F458

Hex in the real world: colour codes

Colours on screens are defined by three values: Red, Green, and Blue, each ranging from 0 to 255. Because 255 = FF in hex, two hex digits are enough for each channel. A web colour like #4ADE80 means:

R: 4A
= 74
G: DE
= 222
B: 80
= 128
#4ADE80
combined

Hex is also used for memory addresses, MAC addresses, IPv6 addresses, and error codes. You will encounter it throughout the course.

New to CSWhen you see a number prefixed with 0x in code (like 0xFF), that is a convention telling you the number is in hexadecimal. The prefix is not part of the value, just a signal to the reader and the compiler. 0xFF = 255 in denary.

Bits, bytes, and beyond

A single binary digit is a bit. Eight bits make a byte. From there, units scale up, but there is an important wrinkle: there are two competing systems.

NameSymbolDecimal definitionBinary definition
KilobyteKB1,000 bytes1,024 bytes (= 2¹&sup0;)
MegabyteMB1,000,000 bytes1,048,576 bytes (= 2²&sup0;)
GigabyteGB1,000,000,000 bytes1,073,741,824 bytes (= 2³&sup0;)
TerabyteTB1,000,000,000,000 bytes1,099,511,627,776 bytes (= 2&sup4;&sup0;)

Hard drive manufacturers use the decimal definitions (so their drives look bigger). Operating systems historically used the binary definitions (so your OS reports a smaller number for the same drive). This is why a "500 GB" hard drive shows as roughly 465 GB in Windows.

The binary-based units have their own names to avoid ambiguity: kibibyte (KiB), mebibyte (MiB), gibibyte (GiB). You will see these in technical documentation, and OCR expects you to know the distinction.

Practice questions

Try each question yourself before revealing the answer.

Q1 Convert 10110011 from binary to denary.
179
128 + 32 + 16 + 2 + 1 = 179
Q2 Convert 214 from denary to 8-bit binary.
11010110
128 + 64 + 16 + 4 + 2 = 214
Q3 Add 01100111 + 00101010 in binary. Give your answer in binary and denary.
10010001 = 145
103 + 42 = 145. Check: 128 + 16 + 1 = 145.
Q4 Represent -35 in 8-bit two's complement.
11011101
+35 = 00100011. Flip: 11011100. Add 1: 11011101. Check: -128+64+16+8+4+1 = -35.
Q5 Convert B7 from hexadecimal to binary and denary.
10110111 = 183
B=1011, 7=0111. So B7 = 10110111. Denary: 128+32+16+4+2+1 = 183.
Q6 A colour is defined as #FF6B00. What are the red, green, and blue values in denary?
Red: 255  |  Green: 107  |  Blue: 0
FF=255, 6B=(6×16)+11=107, 00=0. This is a shade of orange.
Q7 A file is 2.5 GB. How many bytes is this using the binary definition?
2,684,354,560 bytes
2.5 × 2³&sup0; = 2.5 × 1,073,741,824 = 2,684,354,560

Before you move on