Learning objectives
Convert whole numbers between binary, denary and hexadecimal, and explain why hexadecimal is a convenient shorthand for a long string of binary digits
State uses of hexadecimal, including MAC addresses, IPv6 addresses, colour codes, error codes and assembly language instructions
Add two positive binary integers, identify an overflow and describe its effect on the value stored in the register
Apply logical left and right shifts, and represent and interpret negative integers in two’s complement form over a given number of bits
Key definitions
Binary
A base 2 number system that uses only the digits 0 and 1, because an electronic circuit has two reliable states: on and off.
Denary
The base 10 number system used in everyday life, with the ten digits 0 to 9 and place values that are powers of 10.
Hexadecimal
A base 16 number system with the sixteen digits 0 to 9 and A to F, where A is 10 and F is 15. One hexadecimal digit stands for exactly four binary digits.
Nibble
A group of four bits. Because 4 bits give 16 patterns, one nibble is written as one hexadecimal digit.
Overflow
The result of a calculation needs more bits than the register can hold, so the extra bit on the left is lost and the value stored is wrong.
Logical shift
Moving every bit in a register a fixed number of places left or right. Vacated positions are filled with 0 and bits pushed out of the register are lost.
Two’s complement
A way of storing signed integers in which the most significant bit has a negative place value, so the pattern 1 followed by zeros is the most negative number the register can hold.
Explanation
Place values. Every number system gives each column a place value. In denary the places are powers of 10; in binary they are powers of 2, so an 8-bit byte has the place values 128, 64, 32, 16, 8, 4, 2 and 1. To convert binary to denary, add the place values wherever the bit is 1: 10110110 is 128 + 32 + 16 + 4 + 2 = 182. To convert denary to binary, work from the largest place value downwards, writing 1 if it fits and 0 if it does not: for 202, 128 fits leaving 74, 64 fits leaving 10, 32 and 16 do not fit, 8 fits leaving 2, 4 does not fit, 2 fits leaving 0, so 202 is 11001010. With n bits the largest value you can store is 2n − 1, so a byte holds 0 to 255.
Hexadecimal as shorthand. Hexadecimal is base 16, using 0 to 9 then A, B, C, D, E and F for 10 to 15. Because 16 is 24, one hexadecimal digit replaces exactly four binary digits, so a byte is always written as two hexadecimal digits. Split the byte into two nibbles and convert each one: 10110110 becomes 1011 and 0110, which are 11 and 6, so the value is B6. Going the other way, expand each digit into four bits: CA becomes 1100 and 1010, that is 11001010. Hexadecimal is used because it is far shorter and easier for a human to read, write and remember than a long string of 0s and 1s, and mistakes are easier to spot, yet it converts back to binary instantly without any arithmetic.
Where hexadecimal is used. A MAC address is 48 bits shown as six pairs of hexadecimal digits, such as 00-1B-44-11-3A-B7, where the first three pairs identify the manufacturer and the last three identify the device. An IPv6 address is 128 bits shown as eight groups of four hexadecimal digits separated by colons. HTML colour codes use six hexadecimal digits, two for each of red, green and blue, so #3C78D8 means red 60, green 120 and blue 216. Hexadecimal is also used for error codes reported by an operating system, which point the programmer to a memory location, in assembly language and machine code listings, in debugging and memory dumps, and in URLs where a space is written %20.
Binary addition and overflow. Add binary numbers column by column from the right, carrying into the next column: 0 + 0 is 0, 0 + 1 is 1, 1 + 1 is 0 carry 1, and 1 + 1 + 1 is 1 carry 1. For example 10110101 (181) + 01101110 (110) gives 00100011 with a carry of 1 out of the left-hand column. The true answer is 291, but the register can only store 0 to 255, so a ninth bit would be needed. This is an overflow. The carry bit is lost, the stored value becomes 35 instead of 291, and the program either produces a wrong result or the processor raises an error, because the answer has effectively wrapped around past the largest value the register can hold.
Logical shifts. A logical left shift of one place moves every bit one position to the left, puts a 0 into the empty position on the right, and pushes the leftmost bit out of the register, where it is lost. Each place shifted left multiplies the value by 2, provided no 1 is lost: 00110101 (53) shifted left two places is 11010100, which is 212, and 53 × 4 = 212. If a 1 is pushed out the value is no longer a simple doubling: 10011010 (154) shifted left one place gives 00110100, which is 52, not 308. A logical right shift moves every bit right and fills the left with 0, dividing by 2 for each place shifted, with any 1s pushed off the right-hand end lost: 11011000 (216) shifted right three places is 00011011, which is 27, exactly 216 ÷ 8, but 10110011 (179) shifted right two places is 00101100, which is 44 rather than 44.75, because the remainder is thrown away.
Negative numbers in two’s complement. To store a negative value, the most significant bit is given a negative place value, so in 8 bits the places are −128, 64, 32, 16, 8, 4, 2 and 1. The range becomes −128 to 127. To write a negative denary number in this form, write the positive value in binary, invert every bit (change each 0 to 1 and each 1 to 0), then add 1. For −45: 45 is 00101101, inverting gives 11010010, and adding 1 gives 11010011. To read a pattern back, look at the leftmost bit. If it is 0 the number is positive and you convert it normally; if it is 1 the number is negative, so either add the place values including −128, or invert and add 1 and put a minus sign in front. So 10011101 is −128 + 16 + 8 + 4 + 1 = −99, and inverting it gives 01100010 which plus 1 is 01100011, that is 99, confirming −99.
Formulae
Place values in an 8-bit byte: 128, 64, 32, 16, 8, 4, 2, 1
Largest unsigned value in n bits = 2ⁿ − 1, so 8 bits hold 0 to 255
One hexadecimal digit = four binary digits, so one byte = two hexadecimal digits
Two’s complement of a positive number: invert every bit, then add 1
Two’s complement place values in 8 bits: −128, 64, 32, 16, 8, 4, 2, 1 (range −128 to 127)
Logical left shift of n places multiplies by 2ⁿ; logical right shift of n places divides by 2ⁿ, discarding any bits shifted out
Worked examples
q: A register holds the 8-bit value 11001010. (a) Convert it into denary and into hexadecimal. (b) The value is then given a logical right shift of two places. State the new binary pattern and its denary value.
steps: (a) Write the place values 128, 64, 32, 16, 8, 4, 2, 1 above the bits 1, 1, 0, 0, 1, 0, 1, 0.
(a) Add the place values where the bit is 1: 128 + 64 + 8 + 2 = 202, so the denary value is 202.
(a) Split the byte into two nibbles, 1100 and 1010. 1100 is 12, written C, and 1010 is 10, written A, so the hexadecimal value is CA.
(b) A logical right shift of two places moves every bit two positions right and fills the two vacated positions on the left with 0, giving 00110010.
(b) 00110010 is 32 + 16 + 2 = 50. The bits pushed off the right-hand end are lost, so the value is 50 rather than 202 ÷ 4 = 50.5.
In context
Web designers pick colours as six hexadecimal digits because #3C78D8 is far quicker to type and check than the 24 binary digits it stands for.
Every network card leaves the factory with a MAC address printed in hexadecimal, and the first half of it tells a network engineer which company manufactured the card.
Common misconceptions
Students often think hexadecimal is stored in the computer. In fact the computer still stores binary; hexadecimal is only a shorthand that people use when reading or writing those bits.
Students often say an overflow makes the computer add extra bits. In fact the register is a fixed size, so the extra bit is simply lost and the value left behind is wrong.
Students often claim a right shift always divides exactly by 2. In fact any 1 pushed off the right-hand end is lost, so the answer is rounded down.
Students often forget to add 1 after inverting the bits, which gives the one’s complement rather than the two’s complement form.
Study tips
Write the place values above the bits before you convert anything; almost every conversion error comes from losing count of the columns.
When a question gives a number of bits, give your answer in exactly that many bits, padding with leading zeros if necessary.
For an overflow question, state the carry out of the most significant bit and then say what value is actually stored, not just the word "overflow".
Check a two’s complement answer by converting it back: the two methods should agree.
Summary
Binary place values in a byte are 128, 64, 32, 16, 8, 4, 2, 1; n bits hold 0 to 2ⁿ − 1.
One hexadecimal digit equals four bits, so a byte is two hexadecimal digits: it is shorthand for people, not a storage format.
Hexadecimal appears in MAC addresses, IPv6 addresses, colour codes, error codes and assembly language.
Overflow means the answer needs more bits than the register holds, so the carry is lost and the stored value is wrong.
Left shift multiplies, right shift divides, and any bit shifted out of the register is lost.
Two’s complement: invert all bits and add 1; a leading 1 means the value is negative.
Check your understanding
Question 1: The denary value of the hexadecimal number 3F is…
1. 45
2. 315
3. 63
Question 2: An 8-bit register holds 10011010. After a logical left shift of one place it holds…
1. 11001101
2. 00110100
3. 01001101
Question 3: In 8-bit two’s complement form, 11110000 represents…
1. −16
2. −112
3. 240
