Binary, Denary and Hexadecimal
Converting between all three number bases with a method that does not fail under pressure, binary addition including overflow, and binary shifts with their effect on the value.
Binary to denary
Each binary digit has a place value, doubling from right to left.
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 |
Add the place values wherever there is a 1:
128 + 32 + 16 + 2 = 178
So 10110010 is 178 in denary.
Write the place value headings above the digits before you start. Every mark lost on this question type comes from doing it in your head.
Denary to binary
Work from the largest place value downwards. For each, ask: does it fit?
Convert 200:
- 128 fits. Write 1. Remaining: 200 - 128 = 72
- 64 fits. Write 1. Remaining: 72 - 64 = 8
- 32 does not fit. Write 0.
- 16 does not fit. Write 0.
- 8 fits. Write 1. Remaining: 0
- 4, 2, 1 do not fit. Write 0, 0, 0.
Result: 11001000
Always check by adding back: 128 + 64 + 8 = 200. Correct.
- 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.
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.
Hexadecimal
Hexadecimal is base 16. It uses sixteen digits: 0 to 9, then A to F.
| Denary | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Hex | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
Why hexadecimal is used
- It is shorter. One hex digit replaces four binary digits, so a value is a quarter of the length.
- It is easier for humans to read, write and remember, which means fewer mistakes.
- Errors are easier to spot, because a wrong digit stands out in a short string.
- It converts to and from binary very easily, because 16 is a power of 2.
Common uses: colour codes in HTML and CSS, MAC addresses, memory addresses, and error codes.
The computer still stores everything in binary. Hexadecimal is only a shorthand used when people need to read or write binary values. Say this and you get the mark.
Binary to hex
Split the binary into nibbles of four bits, starting from the right, then convert each nibble.
11010110
- Split:
11010110 1101= 8 + 4 + 1 = 13 = D0110= 4 + 2 = 6 = 6- Answer: D6
Hex to binary
Convert each hex digit into its own four bit group.
2F
2=0010F= 15 =1111- Answer:
00101111
Hex to denary
Multiply the first digit by 16 and add the second.
3A = (3 x 16) + 10 = 48 + 10 = 58
Denary to hex
Divide by 16. The quotient is the first digit, the remainder is the second.
93 divided by 16 = 5 remainder 13, so 5D.
- 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.
Binary addition
There are only four rules:
0 + 0 = 0 0 + 1 = 1 1 + 1 = 0 carry 1 1 + 1 + 1 = 1 carry 1
Work from right to left, exactly like column addition in denary.
Worked example
00110101 (53)
- 00011011 (27)
---------- 01010000 (80)
Check: 64 + 16 = 80. Correct.
Overflow
An overflow error occurs when the result of a calculation needs more bits than are available.
11010110 (214)
- 01001010 (74)
---------- 100100000
The answer needs nine bits, but only eight are available. The leading 1 is lost, so the stored answer is wrong.
Overflow occurs when the result of a calculation is too large to be represented in the number of bits available, so the carry out of the most significant bit is lost and the stored result is incorrect.
With 8 bits the largest unsigned value is 255. Any total above that overflows.
- Adding 1111 1111 and 0000 0001 produces a ninth bit. The register only keeps eight, so the answer shown is 0000 0000.
- An odometer is the same: 999 plus 1 gives 000 because the 1000 does not fit in three wheels.
- The answer is wrong, the carry flag is set to record it, and it is avoided with more bits or a check.
Binary shifts
A logical binary shift moves every bit left or right by a given number of places, filling the vacated positions with 0.
Left shift multiplies
Shifting left by n places multiplies the value by 2n.
00001101 (13) shift left 2 00110100 (52)
13 x 4 = 52. Correct, because shifting left 2 multiplies by 22 = 4.
Right shift divides
Shifting right by n places divides the value by 2n.
00110100 (52) shift right 2 00001101 (13)
Loss of data
Bits shifted off the end are lost, and this can cause a loss of precision.
00001101 (13) shift right 2 00000011 (3)
13 divided by 4 is 3.25, but there is no way to store the fraction, so the result is rounded down to 3. The lost bits cannot be recovered.
When asked for the effect of a shift, say both things: the multiplication or division by a power of two, and what happens to bits that fall off the end.
- A left shift moves every bit one column to the left, which doubles it. A 0 fills the gap on the right.
- A right shift moves bits to the right, which halves them each place. Shifting by 2 divides by 4.
- In a fixed number of bits a 1 can be pushed off the end and lost, which makes the answer wrong.
Key terms you must be able to define
- Binary
- Base 2. A number system using only the digits 0 and 1.
- Denary
- Base 10. The everyday number system using digits 0 to 9. Also called decimal.
- Hexadecimal
- Base 16. Uses digits 0 to 9 and letters A to F, where one hex digit represents four bits.
- Most significant bit
- The leftmost bit in a binary number, with the largest place value.
- Least significant bit
- The rightmost bit in a binary number, with a place value of 1.
- Overflow
- An error that occurs when a result requires more bits than are available, so the answer stored is incorrect.
- Logical shift
- Moving all bits left or right by a set number of places, filling the gaps with zeros.
- Nibble
- A group of four bits, represented by exactly one hexadecimal digit.
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
This is the topic where careful students collect free marks and careless ones throw them away. Almost nothing here is difficult, but everything here is easy to get slightly wrong.
Write the place values down every single time. 128 64 32 16 8 4 2 1. Every time, even when you are certain.
Check by converting back. A denary to binary conversion takes ten seconds to verify. Do it.
For hex, always go through binary if you are unsure. Denary to hex directly is fast when it works, but denary to binary to hex is almost impossible to get wrong.
Learn the shift wording precisely. "A left shift of 3 multiplies the number by 8, because 2 to the power 3 is 8. Any bits shifted beyond the most significant bit are lost."
Overflow needs the reason, not the label. Say the result requires more bits than are available, so the most significant bit is lost and the stored value is wrong.
- Convert 8 bit binary to denary in under 20 seconds, reliably
- Convert any two digit hex value to binary and denary
- Add two 8 bit numbers and correctly identify overflow
- State the effect of any shift, including the loss of bits
Mistakes that cost marks here
- Writing place values in the wrong direction. The 1 is on the right, the 128 is on the left.
- Forgetting that hexadecimal A to F represent 10 to 15, and treating A as 1.
- Splitting binary into nibbles from the left rather than the right when the number of bits is not a multiple of four.
- Saying a right shift 'divides by 2 always'. It divides by 2 to the power of the number of places shifted.
- Describing overflow as 'the number is too big' without explaining that the result needs more bits than are available.
- Losing the carry in binary addition, particularly when three 1s are added in one column.
Hexadecimal exists purely for humans. The computer does not use it at all. The colour white as a 24 bit binary value is 111111111111111111111111, and as hex it is FFFFFF, which is why every web developer on Earth is grateful for base 16.
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 method shown, subtracting place values from 128 downwards
- LCorrect answer 11011001
Working from the largest place value: 128 fits into 217 leaving 89, 64 fits into 89 leaving 25, 32 does not fit, 16 fits leaving 9, 8 fits leaving 1, 4 does not fit, 2 does not fit, and 1 fits leaving 0. This gives 11011001. Checking: 128 + 64 + 16 + 8 + 1 = 217.
- LHexadecimal is much shorter, one hex digit replaces four binary digits
- LIt is easier for humans to read, write and remember, so fewer mistakes are made
- LIt converts easily to and from binary because 16 is a power of 2
Hexadecimal is used because it is far more compact than binary: each hexadecimal digit represents exactly four binary digits, so an eight bit value that takes eight characters in binary takes only two in hexadecimal. Shorter values are much easier for a person to read, write down and remember accurately, so programmers make fewer transcription errors and mistakes are easier to spot. Hexadecimal is also convenient because 16 is a power of 2, which means converting between hexadecimal and binary is a simple matter of expanding each digit into its own group of four bits, with no arithmetic required.
- LCorrect addition performed column by column from the right
- LCorrect answer 10100011
- LStates that no overflow error occurs
- LJustifies this because the result fits within 8 bits
Adding column by column from the right and carrying where two or three ones meet gives 10100011. Checking in denary, 01101110 is 110 and 00110101 is 53, and 110 + 53 = 163, which is 10100011 in binary. No overflow error occurs, because the result 163 is still within the range 0 to 255 that eight bits can represent, so there is no carry out of the most significant bit and no data is lost.
- LOccurs when the result of a calculation needs more bits than are available
- LThe carry out of the most significant bit is lost
- LThe stored result is therefore incorrect
- LGives a valid example with 8 bit numbers exceeding 255
An overflow error occurs when the result of a calculation requires more bits than the system has available to store it. When this happens the carry generated out of the most significant bit has nowhere to be stored, so it is lost, and the value that ends up being stored is therefore incorrect. For example, using eight bits the largest value that can be represented is 255. Adding 11010110, which is 214, to 01001010, which is 74, produces 288, and in binary this is 100100000, a nine bit number. Since only eight bits are available, the leading 1 is discarded and the stored result becomes 00100000, which is 32 rather than 288.
- LCorrect result 10110000
- LThe value has been multiplied by 4
- LBecause a left shift of n places multiplies by 2 to the power n
Shifting 00101100 left by two places gives 10110000, with zeros filling the two vacated positions on the right. The original value was 44 and the new value is 176, so the number has been multiplied by 4. This is because a logical left shift of n places multiplies the value by 2 to the power n, and here n is 2, so the multiplier is 4. No bits were shifted beyond the most significant bit in this case, so no data was lost.