Number Systems
Computers store everything as 0s and 1s. This is how whole numbers, positive and negative, are written in binary and hex, and how a computer adds and shifts them.
WATCH IT EXPLAINED
EXPLORE IT IN 3D
The binary console
A steel control panel with one 8-bit register: eight switches, eight lamps, a crank and a keypad. Count, convert, mix a colour in hex, add with carries, shift — and make a number negative.
Why computers use binary
A computer can only hold two states, on and off, so all its data must be binary.
Inside a computer, logic gates process data and registers store it. A logic gate has only two states: on or off. We write them as 1 and 0.
So every value in a computer is a pattern of 1s and 0s. This is binary: base 2, with only two digits, 0 and 1.
That is why all data must be converted to binary before a computer can process it:
- numbers
- text
- sound
- images
- even the program's own instructions
A register holds a fixed number of bits, for example 8 or 16.
Three number systems, one idea: place value
In every number system, each column is worth the base times the column to its right.
| Base | Column values, from the right | |
|---|---|---|
| Denary | 10 | 1, 10, 100, 1000 |
| Binary | 2 | 1, 2, 4, 8 and so on, up to 128 in 8 bits |
| Hexadecimal (hex) | 16 | 1, 16, 256, 4096 |
To find a number's value:
- Multiply each digit by its column's value.
- Add the results.
Remember:At IGCSE, numbers are positive whole numbers only. A binary number has at most 16 bits, so it goes from 0 to 65 535.
Denary to binary: take what fits
To turn denary into binary, go along the place values and take each one that fits.
- Write the place values first: 128, 64, 32 and so on, down to 1.
- Work from left to right.
- Does the place value fit into what is left? Then write 1 and subtract it.
- It does not fit? Write 0.
- Carry on to the last column.
Your answer must have all 8 digits, including any leading zeros. For a 16-bit answer, start at 32 768 instead of 128.
Exam:Any correct method is accepted. Dividing by 2 again and again also works. Show your working.
128 64 32 16 8 4 2 1
1 1 0 0 1 0 0 0
128 fits → 200 − 128 = 72
64 fits → 72 − 64 = 8
32 no, 16 no
8 fits → 8 − 8 = 0
4 no, 2 no, 1 no
answer: 11001000
Or divide by 2 again and again, and read
the remainders from the bottom up.Binary to denary: add up the place values
To turn binary into denary, add up the place values that have a 1 under them.
- Write the place values above the digits.
- Add up the place values that have a 1 underneath.
A 0 adds nothing. But it still holds its column, so do not skip it.
The same method works for 16 bits. The place values keep doubling to the left: 256, 512, 1024 and so on, up to 32 768.
Example:101101 has 1s under 32, 8, 4 and 1. So it is 32 + 8 + 4 + 1 = 45.
32 16 8 4 2 1
1 0 1 1 0 1
32 + 8 + 4 + 1 = 45Hexadecimal digits, and hex to denary
Hex has sixteen digits, 0 to 9 then A to F, and converts like any other base.
Hex needs sixteen digits. After 0 to 9 it uses letters:
| A | B | C | D | E | F |
|---|---|---|---|---|---|
| 10 | 11 | 12 | 13 | 14 | 15 |
Hex to denary: multiply each digit by its place value (1, 16, 256, 4096) and add the results.
Denary to hex:
- Divide by 16 again and again, until the answer is 0.
- Write each remainder as a hex digit.
- Read the remainders from the last one back to the first.
You can also go through binary, as the next note shows. Both methods are correct.
3E8 = 3×256 + 14×16 + 8×1
= 768 + 224 + 8
= 1000
200 ÷ 16 = 12 r 8 → 8
12 ÷ 16 = 0 r 12 → C
read from the bottom up: C8Binary and hex: groups of four
One hex digit is exactly 4 bits, so binary and hex convert in groups of four.
One hex digit is exactly 4 bits, a nibble, because 16 = 2 × 2 × 2 × 2. So you never need to go through denary.
Binary to hex:
- Split the binary into nibbles, starting from the right.
- Is the last group on the left short? Add zeros on its left.
- Convert each nibble on its own, with the place values 8, 4, 2, 1.
Hex to binary: write each hex digit as its 4 bits.
Example:1100 1000 → C and 8 → C8.
1100 1000
C 8 → C8
101101
→ 10 1101 split from the right
→ 0010 1101 pad the left with 0s
→ 2DWhy hexadecimal is used
Computers use only binary; hex is a short way for people to write binary.
Inside the computer, everything is still binary. The computer never uses hex. Hex is for people.
One hex digit stands for 4 bits, so hex is much shorter than binary. That makes it easier for people to read, remember and copy without mistakes. So hex is used wherever people have to read binary values.
Uses you can name in an exam:
- colour codes in HTML and CSS, like
#FF8000 - MAC addresses, like
00-1A-2B-3C-4D-5E - IPv6 addresses, like
2001:db8::1 - error codes and messages
- memory addresses and memory dumps
- assembly language
Adding two binary numbers
Add binary column by column from the right, carrying 1 to the left, just like denary.
Work from right to left, one column at a time. Write each carry above the next column to the left.
| Sum | Write | Carry |
|---|---|---|
| 0 + 0 | 0 | none |
| 0 + 1 | 1 | none |
| 1 + 1 (= 10) | 0 | 1 |
| 1 + 1 + 1 (= 11) | 1 | 1 |
Remember:Check your answer in denary. Convert both numbers and the result: 90 + 60 = 150.
carries 1 1 1 1 0 0 0 0
0 1 0 1 1 0 1 0 90
+ 0 0 1 1 1 1 0 0 60
---------------
1 0 0 1 0 1 1 0 150Overflow
When an answer is too big for the register, the last carry is lost and the stored value is wrong.
A register holds a fixed number of bits. So there is a largest value it can store:
- 8 bits: 255
- 16 bits: 65 535
What if the true answer to an addition is bigger? The last carry has no column to go into, so it is lost. This is an overflow error. The value stored is wrong.
Exam:Say both things. The result is greater than 255 (for 8 bits). The extra bit cannot fit in the register, so the stored value is wrong.
carries 1 1 1 1 1 1 1 0 0
1 0 1 1 0 1 1 0 182
+ 0 1 1 0 1 0 1 1 107
-----------------
1 0 0 1 0 0 0 0 1 289
289 needs 9 bits, but the register
has 8. The carry out of bit 7 is lost,
so it stores 00100001 = 33, which is
289 − 256: wrong.Logical shifts
A logical shift slides every bit left or right; left multiplies by 2, right divides by 2.
A logical shift moves every bit left or right by a number of places.
- Zeros come in at the empty end.
- Bits that go off the other end of the register are lost.
| Shift left | Shift right | |
|---|---|---|
| Each place | multiplies the value by 2 | divides the value by 2 |
| It goes wrong when | a 1 falls off the left end, the most significant bit: the answer is no longer × 2 | a 1 falls off the right end, the least significant bit: the remainder is lost, so the answer is rounded down |
start 00010110 = 22
left 2 01011000 = 88
22 × 4 = 88
left 4 01100000 = 96
not 352: a 1 fell off
right 2 00000101 = 5
22 ÷ 4 = 5.5: the lost
bits held the .5Two's complement: negative numbers
In two's complement, the left-most bit is worth −128, so 8 bits can hold negative numbers.
In two's complement, the left-most bit, the most significant bit (MSB), has a negative place value: −128 in 8 bits.
| Place value | −128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| −45 | 1 | 1 | 0 | 1 | 0 | 0 | 1 | 1 |
The range is −128 to +127. A positive number is ordinary binary and starts with 0. A negative number starts with 1.
To write a negative number:
- Write the positive number in 8 bits.
- Flip every bit.
- Add 1.
To read a pattern back, add up its place values. Include the −128.
−45: 45 = 00101101
flip → 11010010
add 1 → 11010011
check: −128 + 64 + 16 + 2 + 1 = −45KEY TERMS
CHECK YOURSELF
1.What is 1101 in binary as a denary number?
2.Why do people use hexadecimal rather than binary?
3.A number is shifted LEFT by 1 place. What happens to its value?
4.Adding two 8-bit numbers gives a 9-bit result. What is this called?