1.2.4 Numbers
In this lesson, you will learn how denary, binary and hexadecimal number systems are used in computing. You will convert between denary, binary and hexadecimal values, perform binary addition, identify overflow errors, and apply binary shifts to multiply and divide binary values, including explaining when shifts can cause overflow or loss of precision.
Number Systems
Computers represent all data using binary. When working with numerical data, three number systems are particularly important: denary, binary and hexadecimal.
- Denary is the base-10 number system used in everyday life. It uses ten digits, from 0 to 9.
- Binary is a base-2 number system. It uses only the digits 0 and 1, making it suitable for representing the two-state electronic circuits used by computers.
- Hexadecimal is a base-16 number system. It uses the digits 0–9 followed by the letters A–F, where A represents denary 10 and F represents denary 15. Hexadecimal provides a shorter and more convenient way of representing long binary values.
The place value of a digit is the value given to it because of its position in a number. For example, in the denary number 527, the 5 is in the hundreds column and represents 500, the 2 is in the tens column and represents 20, and the 7 is in the ones column and represents 7.
The base of a number system determines how many different symbols it uses and how its place values increase. Denary is base 10, so it uses ten digits (0–9) and each place value is 10 times larger than the one to its right: 1, 10, 100, 1000 and so on. Binary is base 2, so it uses two digits (0 and 1) and each place value is twice the one to its right: 1, 2, 4, 8, 16 and so on. Hexadecimal is base 16, so it uses sixteen symbols and each place value is 16 times larger than the one to its right.
Table 1 below shows how the values 0 to 15 are represented in denary, binary and hexadecimal.
| Denary | Binary | Hexadecimal |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 11 | 1011 | B |
| 12 | 1100 | C |
| 13 | 1101 | D |
| 14 | 1110 | E |
| 15 | 1111 | F |
Binary Place Values
The value of a digit depends on its position within a number. In denary, each column is ten times larger than the column to its right. For example, the number 527 contains 5 hundreds, 2 tens and 7 ones.
Binary works in a similar way, except each column is twice the value of the column to its right because binary is base 2. Starting from the right-hand side, the place values are therefore 1, 2, 4, 8, 16, 32, 64 and 128. These are powers of 2. Each move one column to the left therefore doubles the place value.
For an 8-bit binary number, the place values are as shown in Table 2 below.
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
The leftmost bit is called the most significant bit (MSB) because it occupies the position with the greatest place value. The rightmost bit is called the least significant bit (LSB) because it occupies the position with the smallest place value. In an 8-bit unsigned number, the MSB has a place value of 128 and the LSB has a place value of 1.
Converting Between Denary and Binary
Denary and binary can represent the same numerical values using different place-value systems. Converting between them involves using the binary place values explored above.
Converting Denary to Binary
To convert a positive denary whole number into binary, compare the remaining value with each binary place value from left to right. If a place value is less than or equal to the remaining value, write a 1 in that column and subtract it. If it is greater, write a 0. Continue until the 1 column has been reached.
This method can be visualised using a table, as shown below. Begin by drawing out a table like this:
| 128 () | 64 () | 32 () | 16 () | 8 () | 4 () | 2 () | 1 () |
|---|---|---|---|---|---|---|---|
Starting with the 128 column, compare each place value with the remaining denary value.
- If the place value is greater than the remaining value, write 0.
- If the place value is less than or equal to the remaining value, write 1 and subtract that place value.
Continue from left to right until every column has been completed.
For example, if we want to convert the number 13 to binary, we would follow these steps:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 |
Reading from left to right:
- 128 is greater than 13, so write 0.
- 64 is greater than 13, so write 0.
- 32 is greater than 13, so write 0.
- 16 is greater than 13, so write 0.
- 8 can be subtracted from 13, so write 1. The remaining value is .
- 4 can be subtracted from 5, so write 1. The remaining value is .
- 2 is greater than 1, so write 0.
- 1 can be subtracted from 1, so write 1.
The 1s occur in the 8, 4 and 1 columns, and , which provides a quick check that the conversion is correct.
This gives us the binary number for the denary number 13. Leading zeros do not change the numerical value, so and both represent denary 13. However, when working specifically with an 8-bit value, all eight bit positions are shown.
Converting Binary to Denary
To convert a binary number into denary, place each bit under its corresponding binary place value. Every column containing a 1 contributes its place value to the total, while a column containing 0 contributes nothing. If the binary value has fewer than eight bits, position it from the right-hand side of the table so that its final bit is always placed in the 1 column.
First, draw out a table like the one below:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
Next, take your binary number for conversion, e.g. and place it into the table:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 0 | 1 | 0 | 1 |
Finally, add up the columns that are “on” (i.e. the ones that contain a 1) - in this case:
- The "on" columns are 128, 32, 16, 4, and 1
- Add these values together:
Therefore, the binary number converts to the denary number 181.
As a quick check, the final denary answer should equal the sum of all the place values containing a 1.
Binary Addition and Overflow Errors
Binary addition follows the same column-by-column principle as denary addition, but only the digits 0 and 1 are available. If the total in a column cannot be represented using a single binary digit, part of the result must be carried into the next column to the left.
The rules for binary addition are as follows:
To illustrate these rules, let's perform binary addition for the binary numbers 1101 and 1011 as shown in Figure 1 below.
Working from right to left, each column is added in the same way as denary addition. When a column totals 2, binary represents this as 10, so 0 is written in the current column and 1 is carried into the next column. When a column totals 3, binary represents this as 11, so 1 is written and 1 is carried.
Therefore, the steps carried out in Figure 1 are as follows:
- 1+1=10, so write 0 and carry 1.
- In the next column, , so write 0 and carry 1.
- In the next column, , so write 0 and carry 1.
- In the final column, , so write 1 and carry the remaining 1 into a new column.
The final answer is .
If at least five bits are available, 11000 is simply the correct result. An overflow only occurs if the system is restricted to four bits, because the fifth bit would have nowhere to be stored.
An overflow error occurs when the result of a calculation is too large to be represented using the number of bits available. For an unsigned 8-bit number, the largest value that can be represented is , which is 255 in denary.
For example, consider the following 8-bit addition:
Adding the denary values gives:
However, 299 is greater than the maximum unsigned 8-bit value of 255. The full binary result is:
This requires nine bits. In a fixed 8-bit system, the leftmost carried 1 cannot be stored and is discarded, leaving:
An overflow error has occurred because the correct value, 299, cannot be represented using the eight available bits.
Binary Shifts
A binary shift moves every bit in a binary value a specified number of positions to the left or right.
- In a left shift, every bit moves towards the MSB. Zeros are added at the right-hand side, while any bits that move beyond the left-hand end are discarded. Each left shift normally multiplies an unsigned whole-number value by 2.
- In a right shift, every bit moves towards the LSB. Zeros are added at the left-hand side, while any bits that move beyond the right-hand end are discarded. Each right shift normally divides an unsigned whole-number value by 2.
These multiplication and division effects only give the expected result when no significant 1 bits are discarded during the shift.
However, binary shifts can cause information to be lost when bits are shifted beyond the available eight positions.
Consider the 8-bit value , which represents denary 26. A left shift by one position gives:
The new value is 52:
Starting again with , a right shift by one position gives:
The new value is 13:
In both cases, only 0 bits were discarded, so no information was lost.
Now consider the 8-bit value . Its MSB is 1. Shifting the value one position to the left gives:
represents 178 in denary. If doubling were possible within eight bits, the result would need to represent 356. However, 356 is larger than the maximum unsigned 8-bit value of 255.
The original MSB has moved beyond the available eight bit positions and is discarded. Because a significant 1 has been lost, the stored result is not twice the original value. This is an overflow error caused by the left shift.
A right shift can instead cause a loss of precision when a 1 is discarded from the LSB. For example, represents denary 23. Shifting it one position to the right gives:
The stored result represents denary 11. However:
An unsigned whole-number binary value cannot represent the fractional part (0.5) in this calculation, and the original LSB of 1 has been discarded. The result therefore loses precision.
Converting Between Denary and Hexadecimal
Hexadecimal, often shortened to hex, is a base-16 number system. It uses sixteen symbols: 0–9 followed by A–F, where A represents 10, B represents 11, through to F, which represents 15 as previously explored in Table 1.
Hexadecimal is useful in computing because long binary values can be represented much more compactly. This makes long binary values easier for people to read, write and communicate accurately. Each hexadecimal digit corresponds exactly to one nibble, or four binary bits. This makes conversion between binary and hexadecimal straightforward.
Converting Denary to Hexadecimal
A two-digit hexadecimal number has a 16s column and a 1s column.
| 16 | 1 |
|---|---|
The left-hand digit tells us how many groups of 16 are present, while the right-hand digit tells us how many individual units remain.
If the denary value is below 16 and a two-digit hexadecimal value is required, the 16s digit is 0. For example, denary 9 can be written as hexadecimal 09.
To convert a denary number between 0 and 255 into hexadecimal, divide the denary value by 16.
The whole-number result determines the hexadecimal digit in the 16s column, while the remainder determines the digit in the 1s column. Values from 10 to 15 are represented using the hexadecimal symbols A to F.
For example, convert denary 206 to hexadecimal. First, divide 206 by 16:
This means there are 12 groups of 16, with 14 left over.
Using Table 1:
- Denary 12 is represented by hexadecimal C
- Denary 14 is represented by hexadecimal E
Therefore 206 in denary is equivalent to CE in hexadecimal. The result can be checked using hexadecimal place values:
Converting Hexadecimal to Denary
To convert a two-digit hexadecimal number into denary, first convert each hexadecimal digit into its denary equivalent. The left-hand digit represents the number of 16s, while the right-hand digit represents the number of 1s. This works in the same way as place value in denary and binary: each digit is multiplied by the value of its column.
As shown in Table 3 above, the left-hand digit has a place value of 16 and the right-hand digit has a place value of 1.
For example, convert the hexadecimal number 7B into denary. The first digit is 7, so it contributes . The second digit is B, which represents denary 11, so it contributes .
Multiply each digit by its place value:
Therefore 7B in hexademical is equivalent to 123 in denary.
When a hexadecimal digit contains a letter, remember to convert it to its denary value before carrying out the calculation. For example, A represents 10, B represents 11, and so on up to F, which represents 15.
Converting Between Binary and Hexadecimal
Binary and hexadecimal are closely related because one hexadecimal digit represents exactly four binary bits. A group of four bits is called a nibble, and a nibble can represent 16 possible values from 0000 to 1111. These 16 possible nibble values correspond exactly to the hexadecimal symbols 0 to F.
This means that binary and hexadecimal can be converted directly without first converting the whole value into denary. Each group of four binary bits can be replaced by one hexadecimal digit, and each hexadecimal digit can be replaced by one four-bit binary nibble. Table 1 can be used as a reference when carrying out these conversions.
Converting Binary to Hexadecimal
To convert a binary value into hexadecimal, divide the binary digits into groups of four, called nibbles, starting from the right-hand side. Each nibble is then replaced with its equivalent hexadecimal digit using Table 1. If the number of bits is not a multiple of four, add leading zeros to the left until complete nibbles can be formed. This is called padding.
For example, convert into hexadecimal. First, divide the binary value into two nibbles:
represents denary 13, which is hexadecimal D.
represents denary 10, which is hexadecimal A.
Therefore in binary is equivalent to DA in hexadecimal.
Consider another example where the number of bits is not a multiple of four. The 7-bit value 1011011 can be padded on the left first, so that becomes . Then, follow the conversion steps as normal by dividing the binary value into two nibbles:
0101 represents denary 5, which is also hexadecimal 5.
1011 represents denary 11, which is hexadecimal B.
Therefore in binary is equivalent to 5B in hexadecimal.
Padding by adding leading zeros does not change the numerical value; it simply completes the leftmost nibble.
Converting Hexadecimal to Binary
To convert hexadecimal into binary, replace each hexadecimal digit with its four-bit binary equivalent from Table 1. Because every hexadecimal digit maps to exactly four bits, the conversion can be performed one digit at a time.
For example, convert hexadecimal E4 into binary. The hexadecimal digit E represents denary 14, which is , whereas the hexadecimal digit 4 is .
Place the two nibbles together:
Therefore E4 in hexadecimal is equivalent to in binary.
Each hexadecimal digit must be replaced with exactly four bits, so leading zeros within a nibble must be kept. For example, hexadecimal 4 becomes 0100, not 100. If that leading zero was omitted while converting a multi-digit hexadecimal value, the four-bit grouping would no longer be preserved.
