Binary and Hexadecimal
Why a computer has only two digits to work with, how to convert in both directions without a calculator, and what hexadecimal is actually for.
Why binary
A computer stores and processes everything as binary, a number system with only two digits, 0 and 1. This is not a design preference, it is a consequence of the hardware.
Inside a computer, data is held as an electrical state: a transistor conducting or not conducting, a capacitor charged or discharged, a region of a disc magnetised one way or the other. Each of those is a two state device, so it can represent exactly two values. Call them 0 and 1 and you have binary.
Why not ten states instead
You could in principle build a component with ten distinguishable voltage levels and use denary directly. It would be a bad idea. Two states can be told apart even when the signal is noisy or the voltage drifts, because anything below a threshold reads as 0 and anything above it reads as 1. Ten states would need nine thresholds packed into the same voltage range, so a small amount of electrical interference would produce a wrong value. Binary is used because it is reliable, cheap to manufacture and easy to design circuits for.
The vocabulary
- A bit is a single binary digit, a 0 or a 1.
- A nibble is 4 bits.
- A byte is 8 bits.
An 8 bit binary number can represent 2 to the power 8, that is 256, different values, so as an unsigned integer it covers 0 to 255.
Computers use binary because the components they are built from have two stable states, on and off, and binary digits map directly onto those states, which makes the circuits reliable and simple.
Converting between denary and binary
Place values
An 8 bit binary number has these place values, each double the one to its right:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| 1 | 1 | 0 | 0 | 1 | 0 | 1 | 0 |
Binary to denary
Add up the place values wherever there is a 1. For 11001010: 128 + 64 + 8 + 2 = 202.
Write the place values above the digits before you start. Every mark lost on this question is lost by someone who tried to do it from memory and slipped a column.
Denary to binary
Work from the largest place value down, asking one question at a time: does this fit into what is left?
Converting 202:
| Place value | Does it fit into what remains? | Bit | Remaining |
|---|---|---|---|
| 128 | 128 fits into 202 | 1 | 74 |
| 64 | 64 fits into 74 | 1 | 10 |
| 32 | 32 does not fit into 10 | 0 | 10 |
| 16 | 16 does not fit into 10 | 0 | 10 |
| 8 | 8 fits into 10 | 1 | 2 |
| 4 | 4 does not fit into 2 | 0 | 2 |
| 2 | 2 fits into 2 | 1 | 0 |
| 1 | 1 does not fit into 0 | 0 | 0 |
The answer is 11001010. The remainder reaching zero exactly is your check that you have not made an error.
If the question says 8 bit, write all eight digits. 1010 is a correct value but an incomplete answer; write 00001010.
Number base converter
Convert between denary, binary and hexadecimal, with every step of the working written out. This tool needs JavaScript, so turn it on to use it. Everything you need to answer the exam question is explained in the text above.
- Eight columns, and each one is worth double the one to its right.
- A 1 means take that column's value. A 0 means skip it.
- Add up the columns you took: 64 + 16 + 8 + 2 = 90.
- The leftmost bit is worth more than every other bit added together.
Hexadecimal
Hexadecimal, or hex, is base 16. It uses the digits 0 to 9 and then the letters A to F for the values 10 to 15.
| Denary | 10 | 11 | 12 | 13 | 14 | 15 |
|---|---|---|---|---|---|---|
| Hex | A | B | C | D | E | F |
Why it exists
Hex is not used because computers understand it. They do not: everything is still binary underneath. Hex exists because people make mistakes reading long strings of 0s and 1s.
The number 11001010 is easy to mistype, hard to say out loud and difficult to compare with a similar value at a glance. The same number in hex is CA, two characters. The conversion works because one hex digit represents exactly four bits, so any binary number can be split into nibbles and translated straight across with no arithmetic.
This is why hex is used for MAC addresses, colour codes in web design, memory addresses and machine code listings.
Binary to hex
Split into nibbles of four bits from the right, then convert each nibble.
11001010 splits into 1100 and 1010. 1100 is 8 + 4 = 12, which is C. 1010 is 8 + 2 = 10, which is A. The answer is CA.
Hex to binary
Convert each digit into four bits. 2F becomes 0010 and 1111, so 00101111.
Hex to denary
Multiply the left digit by 16 and add the right digit. CA is (12 multiplied by 16) plus 10, which is 192 + 10 = 202. 2F is (2 multiplied by 16) plus 15 = 47.
Denary to hex
The safe route is through binary: convert to 8 bit binary, split into two nibbles, convert each. Converting 181 directly is error prone, but 181 is 10110101, which splits into 1011 and 0101, giving B5.
In an exam, denary to hex and hex to denary are both quicker and safer via binary, because you already know the place values cold and there is no multiplication to slip up on.
- Split the byte into two nibbles of four bits. Colours keep each nibble with its digit.
- Convert each nibble on its own with 8, 4, 2 and 1: 1010 is 10 and 1111 is 15.
- Write each value as one hex digit, 10 as A and 15 as F, and put them side by side.
- Learn the table: 0 to 9 look the same in hex, then A is 10 up to F which is 15.
Key terms you must be able to define
- Binary
- A number system with base 2 that uses only the digits 0 and 1.
- Bit
- A single binary digit, either 0 or 1, and the smallest unit of data a computer can store.
- Nibble
- A group of four bits, which is exactly the amount represented by one hexadecimal digit.
- Byte
- A group of eight bits, able to represent 256 different values.
- Denary
- The everyday number system with base 10, using the digits 0 to 9.
- Hexadecimal
- A number system with base 16 that uses the digits 0 to 9 and the letters A to F.
- Place value
- The value a digit represents because of its position, doubling with each move to the left in binary.
- Unsigned integer
- A whole number with no sign bit, so an 8 bit unsigned integer represents values from 0 to 255.
- Most significant bit
- The leftmost bit of a binary number, which carries the largest place value.
Flashcards
Say the definition out loud before you flip the card. Retrieving it from memory is what builds recall, reading it again is not.
Reach the top grade on this topic
Conversions are marked right or wrong, so accuracy under pressure is the whole skill.
Write the place values down every time. Every single time, even when you are sure. It costs four seconds and it is the difference between 202 and 234.
Check your answer by converting it back. If you convert 181 to 10110101, add up 128 + 32 + 16 + 4 + 1 and confirm you get 181. Two conversions take less than a minute and turn a guess into a certainty.
Explain hexadecimal in terms of people, not machines. The reason hex exists is that humans read and write it more reliably than binary, and one hex digit maps to exactly four bits so the translation needs no calculation. An answer claiming that computers use hex, or that hex saves storage space, is wrong: the value in memory is identical.
Be precise about ranges. An 8 bit unsigned value covers 0 to 255, which is 256 different values. Saying "up to 256" is the classic slip.
- Be able to convert any denary value from 0 to 255 into 8 bit binary in under thirty seconds
- Be able to convert 8 bit binary to denary and to hexadecimal
- Be able to convert two hexadecimal digits to binary and to denary
- Be able to give three real uses of hexadecimal and explain why hex rather than binary is used there
Mistakes that cost marks here
- Reading binary place values from the left as 1, 2, 4, 8. The place values increase from right to left, so the leftmost bit of a byte is 128.
- Giving a binary answer with fewer than eight bits when the question specifies an 8 bit number.
- Saying computers use hexadecimal. Computers use binary. Hex is a shorthand for people reading and writing binary values.
- Claiming hexadecimal saves memory. CA and 11001010 occupy exactly the same eight bits in the machine.
- Saying an 8 bit unsigned number can store values up to 256. It stores 256 different values, from 0 to 255.
- Splitting binary into nibbles from the left rather than the right, which gives the wrong answer whenever the number has fewer than eight digits.
A single hexadecimal digit represents exactly four bits, which is why hex is used for colour codes and MAC addresses. The colour written as FF8000 is 11111111 10000000 00000000 in binary, which is why nobody writes colours in binary.
Knowledge check
Exam-style questions
Five written questions in the style of the real paper. Write a full answer, then let the marker check it against the mark scheme.
- LCorrect binary answer 10011100
- LWorking shown using place values, subtracting 128, 16, 8 and 4
Starting from the highest place value, 128 fits into 156 and leaves 28, so the first bit is 1. 64 does not fit into 28 and 32 does not fit into 28, so the next two bits are 0. 16 fits into 28 and leaves 12, 8 fits into 12 and leaves 4, and 4 fits into 4 and leaves 0, so those three bits are 1. The final two place values, 2 and 1, do not fit into 0, so both are 0. The answer is 10011100.
- LComputer components such as transistors have two states, on and off
- LThese two states map directly onto the digits 0 and 1
- LTwo states can be distinguished reliably even with electrical noise, so circuits are simpler and less error prone
The components a computer is built from, such as transistors and memory cells, are two state devices: they are either conducting or not conducting, charged or discharged. Those two states map directly onto the two digits of binary, 0 and 1, so no translation is needed between the data and the hardware holding it. Two states are used rather than ten because a circuit only has to distinguish between a voltage above a threshold and one below it, which remains reliable even when the signal is affected by electrical noise or by voltage drifting slightly, and circuits that only have to make that one distinction are simpler and cheaper to manufacture.
- LSplits the binary number into the nibbles 1110 and 0101
- LCorrectly gives the hexadecimal answer E5
- LAdds the place values 128, 64, 32, 4 and 1
- LCorrectly gives the denary answer 229
To convert to hexadecimal the byte is split into two nibbles from the right, giving 1110 and 0101. The nibble 1110 is 8 plus 4 plus 2, which is 14, and 14 is written as E in hexadecimal. The nibble 0101 is 4 plus 1, which is 5. The hexadecimal value is therefore E5. To convert to denary the place values where a 1 appears are added together: 128 plus 64 plus 32 plus 4 plus 1, which gives 229. This can be checked against the hexadecimal answer, since E is 14 and 14 multiplied by 16 is 224, and 224 plus 5 is 229.
- LOne hexadecimal digit represents exactly four bits
- LSo binary values can be written far more briefly, two digits instead of eight
- LShorter values are easier for people to read, write, remember and compare, so fewer mistakes are made
- LConversion between hexadecimal and binary is direct and requires no calculation
Hexadecimal is base 16, and because 16 is 2 to the power 4, one hexadecimal digit represents exactly four bits. That relationship means a byte can always be written as two hexadecimal digits, so a value such as 11001010 becomes simply CA. Programmers work with binary values constantly, in memory addresses, colour codes and MAC addresses, and long strings of ones and zeros are extremely easy to misread, mistype or lose your place in. Writing the same value in a quarter of the characters makes it far easier to read aloud, note down and compare with another value. The conversion is also completely mechanical: each group of four bits translates straight into one hexadecimal digit with no arithmetic at all, so nothing is lost or risked in moving between the two.
- LAll data is stored in the computer as binary regardless of how it is written down
- LHexadecimal is a way of writing or displaying a value for people, not a way of storing it
- LThe value CA and the value 11001010 occupy the same eight bits in memory
The claim is incorrect because every value inside a computer is held as binary, whatever notation a person uses when writing it down. Hexadecimal is a human readable shorthand for binary, not a separate storage format, so writing a byte as CA rather than as 11001010 changes nothing about the memory the value occupies. Both notations describe the same eight bits, and those eight bits take up one byte of memory in either case. What hexadecimal saves is not space in the machine but effort and errors for the person reading and writing the value.