Why computers use binary
A computer is made of billions of tiny switches called transistors. Each switch is either off or on.
We write off as 0 and on as 1. A number system with only two digits is called binary.
One binary digit is a bit. Eight bits together make a byte.
Every extra bit doubles the number of different patterns you can make.
e.g.Example1 bit: 0, 1 → 2 patterns
2 bits: 00, 01, 10, 11 → 4 patterns
3 bits: 000, 001, 010, 011, 100, 101, 110, 111 → 8 patterns
8 bits: 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 = 256 patterns
?Why?Two states are very reliable. A switch is clearly on or clearly off, so the computer rarely misreads a value.
Place value: denary and binary
Denary is our everyday number system. It has ten digits, 0 to 9, and each column is worth 10 times the column to its right.
Binary works the same way, but each column is worth 2 times the column to its right.
For 8 bits the column values, from left to right, are 128, 64, 32, 16, 8, 4, 2, 1.
To turn binary into denary, add up the column values that have a 1 under them.
Try the toy: switch on the 128 bit. What happens to the denary value? Now make 255.
e.g.ExampleBinary 01011010 to denary.
The 1s sit under 64, 16, 8 and 2.
64 + 16 + 8 + 2 = 90
!Watch outAlways write the column values above the bits first. Most mistakes come from adding the wrong column.
There is something to try for this step in the IGCSE library.
Denary to binary
Start at the left column, 128. Ask: does 128 fit into my number?
If it fits, write 1 and take 128 away. If it does not fit, write 0.
Move to the next column and repeat with what is left. Stop when you reach the 1 column.
Always write all 8 bits, including the 0s at the front.
e.g.ExampleDenary 150 to binary.
128 fits → 1. Left: 150 − 128 = 22
64 does not fit → 0
32 does not fit → 0
16 fits → 1. Left: 22 − 16 = 6
8 does not fit → 0
4 fits → 1. Left: 6 − 4 = 2
2 fits → 1. Left: 0
1 does not fit → 0
Answer: 10010110
Check: 128 + 16 + 4 + 2 = 150
?Why?Each column is used once at most, so taking away the biggest column first always gives the right pattern.
The biggest number n bits can hold
With n bits you get 2ⁿ patterns. One of those patterns is 0.
So the biggest number is one less than the number of patterns: 2ⁿ − 1.
8 bits hold 0 to 255. 16 bits hold 0 to 65,535.
A register is a small store inside the processor. An 8-bit register cannot hold anything bigger than 255.
e.g.Example8 bits: 2⁸ = 256 patterns. Biggest = 256 − 1 = 255 (11111111)
16 bits: 2¹⁶ = 65,536 patterns. Biggest = 65,536 − 1 = 65,535 (1111111111111111, or FFFF in hex)
Quick check for 8 bits: 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255
!Watch out8 bits give 256 different values, but the largest value is 255, because counting starts at 0.
Hexadecimal: base 16
Hexadecimal (hex) has sixteen digits. It uses 0 to 9, then the letters A to F for 10 to 15.
Each hex column is worth 16 times the column to its right. Two-digit hex has columns 16 and 1. Three digits add a 256 column.
One hex digit stands for exactly four bits. Four bits are called a nibble.
?Why?16 is 2 × 2 × 2 × 2. That is why one hex digit matches four bits exactly, with nothing left over.
Hex to denary and denary to hex
Hex to denary: multiply each digit by its column value, then add. Change letters to numbers first (B = 11, F = 15).
Denary to hex: divide by 16. The whole-number answer is the left digit. The remainder is the right digit.
Change any digit from 10 to 15 into its letter.
e.g.ExampleHex B7 to denary:
B = 11, so 11 × 16 = 176
7 × 1 = 7
176 + 7 = 183
Denary 200 to hex:
200 ÷ 16 = 12 remainder 8
12 = C, so the answer is C8
Check: 12 × 16 + 8 = 192 + 8 = 200
Three digits, hex 1A3 to denary:
1 × 256 = 256
A = 10, so 10 × 16 = 160
3 × 1 = 3
256 + 160 + 3 = 419
!Watch outA remainder of 10 to 15 must be written as a letter. Writing "128" for C8 is a common slip.
Binary and hex using nibbles
Binary to hex: split the binary into groups of 4 bits, starting from the right. Change each group into one hex digit.
Hex to binary: change each hex digit into its 4-bit nibble. Write them side by side.
Each nibble uses the small column values 8, 4, 2, 1. This keeps the sums easy.
The fastest route between denary and hex is often through binary.
e.g.ExampleBinary 10110110 to hex:
Split: 1011 | 0110
1011 = 8 + 2 + 1 = 11 = B
0110 = 4 + 2 = 6
Answer: B6
Hex 4E to binary:
4 = 0100
E = 14 = 8 + 4 + 2 = 1110
Answer: 01001110
12 bits, 000110100011 to hex:
0001 | 1010 | 0011 → 1, A, 3 → 1A3
!Watch outAlways group from the right. If the left group is short, add 0s at its front. Keep the leading 0s when you change hex to binary, so each digit gives four bits.
Why hexadecimal is used
Computers do not use hex. Inside, everything is still binary. Hex is for people.
Hex is shorter: 8 bits become 2 hex digits. It is easier to read, remember and type.
People make fewer mistakes copying hex than long strings of 0s and 1s.
It converts to binary very easily, because each digit is one nibble.
!Watch outDo not write "hex takes up less memory". It does not. The computer stores the same binary. Hex is only a shorter way for humans to write it.
Adding binary: the four rules
You add binary in columns, from right to left, like denary addition.
In binary, 1 + 1 makes 2, and 2 is written 10. So you write 0 and carry 1 to the next column.
If a column has 1 + 1 plus a carry of 1, the total is 3. Three is 11: write 1 and carry 1.
Try the toy: it starts with 91 + 54. Press Step to add one column at a time and watch where each carry goes. Then press Overflow example.
There is something to try for this step in the IGCSE library.
A full 8-bit addition
Write the two numbers one under the other, with the column values on top.
Write each carry above the next column on the left. Then add that column, including the carry.
Check your answer by changing everything to denary.
e.g.ExampleColumn 1: 1 + 0 = 1
Column 2: 0 + 1 = 1
Column 4: 1 + 1 = 10 → write 0, carry 1
Column 8: 1 + 0 + carry 1 = 10 → write 0, carry 1
Column 16: 0 + 1 + carry 1 = 10 → write 0, carry 1
Column 32: 1 + 1 + carry 1 = 11 → write 1, carry 1
Column 64: 1 + 0 + carry 1 = 10 → write 0, carry 1
Column 128: 0 + 0 + carry 1 = 1
Answer: 10100011
Check: 128 + 32 + 2 + 1 = 163, and 109 + 54 = 163
!Watch outShow your carries in the exam. A wrong answer with clear carries can still earn a working mark.
Overflow
An 8-bit register can hold 0 to 255. Sometimes an addition gives more than 255.
Then the last column makes a carry that has nowhere to go. It would need a 9th bit.
This is called an overflow error. The 9th bit is lost, so the stored answer is wrong.
The processor sets an overflow flag so the program can deal with it.
e.g.Example200 + 100 = 300. But 300 is more than 255.
The full answer needs 9 bits: 1 00101100.
The register keeps only the right 8 bits: 00101100 = 32 + 8 + 4 = 44.
300 − 256 = 44, because the lost 9th bit was worth 256.
?Why?A register has a fixed number of bits. Anything that does not fit is lost, so the computer cannot store a value above 2ⁿ − 1.
Logical left shift
A logical shift moves every bit left or right by a number of places.
In a left shift, bits fall off the left end and are lost. 0s fill the gaps on the right.
Each place shifted left multiplies the number by 2. Two places is × 4. Three places is × 8.
If a 1 falls off the left end, the answer is wrong. This is a kind of overflow.
Try the toy: load 22 (00010110), choose 2 places and press Shift left. Then load 53 (00110101) and shift it left 3 places. Which answer is wrong?
e.g.Example22 shifted left 2 places:
00010110 → 01011000
0 and 0 fall off the left. Two 0s come in on the right.
01011000 = 64 + 16 + 8 = 88. And 22 × 4 = 88. Correct.
53 shifted left 3 places:
00110101 → 10101000
0, 0 and 1 fall off the left. The lost 1 was important.
10101000 = 128 + 32 + 8 = 168. But 53 × 8 = 424. Wrong answer, because 424 is too big for 8 bits.
?Why?Moving a bit one column left puts it in a column worth twice as much. So the whole number doubles.
There is something to try for this step in the IGCSE library.
Logical right shift
In a right shift, bits fall off the right end and are lost. 0s fill the gaps on the left.
Each place shifted right divides the number by 2. Two places is ÷ 4. Three places is ÷ 8.
If a 1 falls off the right end, the remainder is lost. The answer is rounded down, so accuracy is lost.
e.g.Example180 shifted right 1 place:
10110100 → 01011010 = 64 + 16 + 8 + 2 = 90. And 180 ÷ 2 = 90. Exact.
22 shifted right 2 places:
00010110 → 00000101 = 4 + 1 = 5
But 22 ÷ 4 = 5.5. The bits 1 and 0 fell off the right end.
The lost 1 was worth 0.5 after dividing, so the answer is not exact.
!Watch outIn a logical shift the new bits are always 0. Do not copy the old end bit back in, and do not wrap bits round to the other end.
Two's complement: negative numbers
So far 8 bits only held positive numbers, 0 to 255. Two's complement is a way to store negative numbers too.
The only change: the leftmost column is worth −128 instead of 128. All other columns stay the same.
If the leftmost bit is 0, the number is positive. If it is 1, the number is negative.
8-bit two's complement holds −128 to +127.
Try the toy: press Smallest (−128) so only the −128 bit is on. Then tap the 64 bit. Watch the value climb towards 0.
e.g.ExampleTwo's complement 10110110 to denary.
The 1s are under −128, 32, 16, 4 and 2.
−128 + 32 + 16 + 4 + 2 = −128 + 54 = −74
?Why?With a −128 column, normal binary addition still works for negative numbers. The processor needs only one adding circuit.
There is something to try for this step in the IGCSE library.
Denary to two's complement
Positive number: write it in normal 8-bit binary. The leftmost bit will be 0.
Negative number, method 1: write the positive version in binary. Flip every bit (0 becomes 1, 1 becomes 0). Then add 1.
Negative number, method 2: start with −128. Work out what you must add to reach your number. Put that in the other 7 columns.
Use one method and check with the other, or check by adding the column values.
e.g.ExampleChange −45 into 8-bit two's complement.
Method 1 (flip and add 1):
+45 = 00101101
Flip every bit: 11010010
Add 1: 11010011
Method 2 (start at −128):
−45 = −128 + 83
83 = 64 + 16 + 2 + 1
So: 1 in the −128 column, then 1010011
Answer: 11010011
Check: −128 + 64 + 16 + 2 + 1 = −45
!Watch out+45 and −45 are not the same pattern with a different first bit. 10101101 is −83, not −45. Always flip and add 1, or use the −128 method.