KS4J277 1.2.4 35 min read 10 question quiz 16 exam marks

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.

1286432168421
10110010

Add the place values wherever there is a 1:

128 + 32 + 16 + 2 = 178

So 10110010 is 178 in denary.

Method that never fails

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.

Converting binary 01011010 to denary using place valuesAn eight bit binary number laid out under its place values. The place values from left to right are 128, 64, 32, 16, 8, 4, 2 and 1, each half the one to its left. The bits are 0, 1, 0, 1, 1, 0, 1, 0. Where a bit is 1 the column contributes its place value, so the contributions are 0, 64, 0, 16, 8, 0, 2 and 0. Adding the contributions gives 64 plus 16 plus 8 plus 2, which is 90. So binary 01011010 is 90 in denary.1280064164320016116818400212100placebitworth64 + 16 + 8 + 2 = 90so 01011010 in binary is 90 in denary
Write the place values above the bits every single time. It costs five seconds and removes almost every conversion error.
The eight place value columns of a byte, here holding 0101 1010The eight place value columns of a byte, drawn as blocks whose height is the value of the column: 128, 64, 32, 16, 8, 4, 2 and 1 from left to right, each one worth double the column to its right. Above each column is a light that is on for a 1 and off for a 0. To read a binary number, take the value of every column holding a 1 and ignore every column holding a 0. For 0101 1010 that means taking 64, 16, 8 and 2, which add up to 90. The tallest block shows something worth remembering: the leftmost column, 128, is worth more than all of the other seven added together, which come to 127.0128skip164032skip1161804skip1201skipa 1 means take the column, a 0 means skip it128 is worth more than all the rest together (127)64 + 16 + 8 + 2 = 901234
  1. Eight columns, and each one is worth double the one to its right.
  2. A 1 means take that column's value. A 0 means skip it.
  3. Add up the columns you took: 64 + 16 + 8 + 2 = 90.
  4. The leftmost bit is worth more than every other bit added together.
Each column doubles. Learn the row 128 64 32 16 8 4 2 1 and every conversion question becomes adding up.

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.

Denary0123456789101112131415
Hex0123456789ABCDEF

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.

Hex is for humans, not computers

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: 1101 0110
  • 1101 = 8 + 4 + 1 = 13 = D
  • 0110 = 4 + 2 = 6 = 6
  • Answer: D6

Hex to binary

Convert each hex digit into its own four bit group.

2F

  • 2 = 0010
  • F = 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.

Converting 1010 1111 to hexadecimal one nibble at a time, and the sixteen hex digitsHow to convert a byte to hexadecimal. The byte 1010 1111 is split into two nibbles of four bits each: 1010 on the left and 1111 on the right. Each nibble is converted on its own using the place values 8, 4, 2 and 1. The left nibble is 8 plus 2, which is 10, and 10 is the hex digit A. The right nibble is 8 plus 4 plus 2 plus 1, which is 15, and 15 is the hex digit F. Written side by side the byte is AF in hex. Underneath is a table of all sixteen hex digits with their denary and four bit binary values: 0 to 9 are written as themselves and 10 to 15 are written A, B, C, D, E and F.Binary to hex, one nibble at a time101011118 + 2 = 10A8 + 4 + 2 + 1 = 15FAFside by sideThe sixteen hex digitshexdenarybinary000000110001220010330011440100550101660110770111881000991001A101010B111011C121100D131101E141110F151111One hex digit is always exactly four bits.1234
  1. Split the byte into two nibbles of four bits. Colours keep each nibble with its digit.
  2. Convert each nibble on its own with 8, 4, 2 and 1: 1010 is 10 and 1111 is 15.
  3. Write each value as one hex digit, 10 as A and 15 as F, and put them side by side.
  4. Learn the table: 0 to 9 look the same in hex, then A is 10 up to F which is 15.
Split into nibbles, convert each, join them up. Going the other way, turn each hex digit back into four bits.

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.

The definition

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.

Binary addition with carries, and overflowA worked binary addition set out in columns. The first number is 01101101, which is 109 in denary. The second is 00110110, which is 54. Working from the right, the four rules are that 0 plus 0 is 0, 1 plus 0 is 1, 1 plus 1 is 0 carry 1, and 1 plus 1 plus 1 is 1 carry 1. Carries of 1 are generated in four of the columns. The result is 10100011, which is 163 in denary, and 109 plus 54 does equal 163. Overflow happens when a carry comes out of the leftmost column: with only eight bits available there is no ninth bit to hold it, so the carry is lost and the stored answer is wrong.Adding two 8 bit numbers, and what overflow meanscarry1111101101101= 109+00110110= 54=10100011= 163The four rules0 + 0 = 0 1 + 0 = 11 + 1 = 0 carry 11 + 1 + 1 = 1 carry 1OverflowIf a carry comes out of the leftmostcolumn there is no ninth bit to hold it,so the answer stored is wrong.
Always write the carry row above the columns. Losing a carry off the left hand end is overflow, and it is a favourite exam question.
Overflow: 255 plus 1 does not fit in eight bitsWhat overflow is. Adding 1111 1111, which is 255, and 0000 0001 in an eight bit register gives 1 0000 0000, which is 256 and needs nine bits. The register only holds eight, so the ninth bit, the leading 1, has nowhere to go and is lost, and the register keeps 0000 0000, which is 0. The answer is wrong. An odometer does the same thing: 999 plus 1 turns all three wheels to 0 and the 1000 does not fit. When this happens the processor sets the carry flag in the status register. It can be avoided by using more bits, or by checking the flag.255 + 1 in an 8 bit register1111111100000001+000000001what the register keeps: 09th bit: no roomThe same thing on an odometer999+ 10001000doesnot fitWhat goes wrongThe answer is wrong255 + 1 shows as 0,not 256.A flag is setThe carry flag in thestatus registerrecords it.How to avoid itUse more bits, orcheck the flag.123
  1. Adding 1111 1111 and 0000 0001 produces a ninth bit. The register only keeps eight, so the answer shown is 0000 0000.
  2. An odometer is the same: 999 plus 1 gives 000 because the 1000 does not fit in three wheels.
  3. The answer is wrong, the carry flag is set to record it, and it is avoided with more bits or a check.
Overflow happens when a result needs more bits than the register has. The largest 8 bit number is 255.

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.

The mark scheme wording

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.

Binary shifts: left multiplies by two, right divides, and a bit can be lostThree worked binary shifts on an eight bit number. First, shifting 0000 0110, which is 6, left by one place gives 0000 1100, which is 12: every bit moves one column left, which doubles its value, and a new 0 fills the gap on the right. Second, shifting 0001 1000, which is 24, right by two places gives 0000 0110, which is 6: every bit moves two columns right, which divides the value by four, and two new 0s fill the gaps on the left. Third, a warning: shifting 1100 0000, which is 192, left by one place pushes the leading 1 off the end of the register, so the result is 1000 0000, which is 128, not 384. A bit that is shifted off the end is lost.Shift left by 1 place: × 20000011000001100<< 1× 2= 6= 12Shift right by 2 places: ÷ 40001100000000110>> 2÷ 4= 24= 6Careful: a 1 can fall off the end11000000100000001<< 1bit lost= 192= 128, not 384a 0 is shifted ina bit is lost off the end123
  1. A left shift moves every bit one column to the left, which doubles it. A 0 fills the gap on the right.
  2. A right shift moves bits to the right, which halves them each place. Shifting by 2 divides by 4.
  3. In a fixed number of bits a 1 can be pushed off the end and lost, which makes the answer wrong.
Left shift by n multiplies by 2 to the power n; right shift divides. It only works while no 1 falls off the end.

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.

DefineBinary
AnswerBase 2. A number system using only the digits 0 and 1.
DefineDenary
AnswerBase 10. The everyday number system using digits 0 to 9. Also called decimal.
DefineHexadecimal
AnswerBase 16. Uses digits 0 to 9 and letters A to F, where one hex digit represents four bits.
DefineMost significant bit
AnswerThe leftmost bit in a binary number, with the largest place value.
DefineLeast significant bit
AnswerThe rightmost bit in a binary number, with a place value of 1.
DefineOverflow
AnswerAn error that occurs when a result requires more bits than are available, so the answer stored is incorrect.
DefineLogical shift
AnswerMoving all bits left or right by a set number of places, filling the gaps with zeros.
DefineNibble
AnswerA group of four bits, represented by exactly one hexadecimal digit.

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

Avoid these
  • 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.
Fun fact

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

0 of 10 answered
1What is the denary value of the binary number 10011010?
2Convert the denary number 172 to 8 bit binary.
3What is 11101001 in hexadecimal?
4What is the denary value of hexadecimal 4C?
5Why is hexadecimal used by programmers?
600110101 shifted left by 2 places gives which result, and what has happened to the value?
7An overflow error occurs when:
8What is 01010110 + 00101101 in binary?
9How many bits does one hexadecimal digit represent?
1000011000 is shifted right by 3 places. What is the result in denary and what has happened?
Score 0 / 10

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.

16 marks
1Convert the denary number 217 into 8 bit binary. Show your working.2 marks
Convert

Mark scheme0 / 2
  • LCorrect method shown, subtracting place values from 128 downwards
  • LCorrect answer 11011001
Model answer

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.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
2Explain why hexadecimal is often used by programmers instead of binary.3 marks
Explain

Mark scheme0 / 3
  • 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
Model answer

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.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
3Add the binary numbers 01101110 and 00110101. Show your working and state whether an overflow error occurs.4 marks
Add

Mark scheme0 / 4
  • 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
Model answer

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.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
4Explain what is meant by an overflow error, using an example.4 marks
Explain

Mark scheme0 / 4
  • 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
Model answer

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.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
5The binary number 00101100 is shifted left by 2 places. State the result and explain the effect this has on the value represented.3 marks
State

Mark scheme0 / 3
  • LCorrect result 10110000
  • LThe value has been multiplied by 4
  • LBecause a left shift of n places multiplies by 2 to the power n
Model answer

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.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
Machine mark 0 / 16