Pages: <Previous  2    3    4    5    6    7    8    9    10    11    12  Next >

2 Representing data in the kitchen scales

2.2.3 Positve integers: converting denary numbers to binary

If computers encode the denary numbers of the everyday world as binary numbers, then clearly there needs to be conversion from denary to binary and vice versa. You have just seen how to convert binary numbers to denary, because I did a couple of examples to show you how binary numbers ‘work’. But how can denary numbers be converted to binary? I'll show you by means of an example.

Example 1

Convert 219 to an 8-bit binary number.

Answer

First write out the weightings of an 8-bit number:

27 26 25 24 23 22 21 20
(128) (64) (32) (16) (8) (4) (2) (1)

Now decide whether the given number, 219, is larger than or equal to the largest weighting, 128. If the given number is larger than or equal to the weighting, record a 1 in the ‘128’ column and subtract the weighting from the number. But if the given number is smaller than the weighting, record a 0 in the ‘128’ column.

Here 219 is bigger than 128. Hence a 1 goes into the ‘128’ column (there is one ‘128’ in 219). This is shown below. The result of the subtraction 219 − 128 is 91.

27 26 25 24 23 22 21 20
(128) (64) (32) (16) (8) (4) (2) (1)
1              

Now decide whether the result of the subtraction, 91, is larger than or equal to than the next-lower weighting, 64. (I'll just call the result of the subtraction ‘the new number’.) If the new number is larger than or equal to the weighting, record a 1 in the ‘64’ column and subtract the weighting from the new number. If the new number is smaller than the weighting, record a 0 in the ‘64’ column.

Here 91 is larger than 64. Hence a 1 goes into the ‘64’ column (there is one ‘64’ in 91). This is shown below. The result of the subtraction 91 − 64 is 27.

27 26 25 24 23 22 21 20
(128) (64) (32) (16) (8) (4) (2) (1)
1 1            

Now decide whether the new number, 27, is larger than or equal to the next-lower weighting, 32. If the new number is larger than or equal to the weighting, record a 1 in the ‘32’ column and subtract the weighting from the new number. If the new number is smaller than the weighting, record a 0 in the ‘32’ column.

Here 27 is smaller than 32. Hence a 0 goes into the ‘32’ column (there are no ‘32s’ in 27):

27 26 25 24 23 22 21 20
(128) (64) (32) (16) (8) (4) (2) (1)
1 1 0          

Now decide whether the number brought forward, 27, is larger than or equal to the next-lower weighting, 16. If the number brought forward is larger than or equal to the weighting, record a 1 in the ‘16’ column and subtract the weighting from the new number. If the number brought forward is smaller than the weighting, record a 0 in the ‘16’ column.

Here 27 is larger than 16. Hence a 1 goes into the ‘16’ column (there is one ‘16’ in 27). This is shown below. The result of the subtraction 27 − 16 is 11.

27 26 25 24 23 22 21 20
(128) (64) (32) (16) (8) (4) (2) (1)
1 1 0 1        

The process continues for the last four remaining weightings, and you can check for yourself that the final result is:

27 26 25 24 23 22 21 20
(128) (64) (32) (16) (8) (4) (2) (1)
1 1 0 1 1 0 1 1

The binary number can now be read off; it is 1101 1011.

Sometimes the first bit, or the first few bits, turn out to be 0 – as would be the case if, say, denary 6 was being converted to an 8-bit number. These ‘leading zeros’ must be included in the resulting binary number, otherwise it would not be 8 bits long. So, for example, 6 is 0000 0110 in 8-bit representation.

The same principles as those illustrated in the example above apply to other word lengths; the important point is to start the comparison-and-subtraction process with the weighting of the most-significant bit for the word length you are using.

Activity 6 (Self assessment)

(a) Convert denary 7 into a 4-bit number.
(b) Convert the following denary numbers into 8-bit numbers:
(i) 120
(ii) 13

Now read the answer

Activity 7 (Exploratory)

Is there more than one possible result of converting a denary number into a binary number? Look back at the conversion of 219 (my example) and of 7, 120 and 13 (Activity 6) to help you to decide.

Now read the discussion

In conclusion, then, if the denary number to be encoded is a positive integer (a positive whole number), then the number is converted to binary form, as just described, so that it can be stored or manipulated in the computer. And after manipulation the result can be converted back to denary for reporting the outcome to the user. In the kitchen scales, for example, the weight is displayed as a denary number on the display.

Box 1: Distinguishing between binary and denary numbers

Very often it's obvious whether a given number is binary or denary: 2975 is denary, and 0011 1010 is very likely (though not absolutely certain) to be binary. But is the number 1001 binary or denary?

One way of distinguishing clearly between binary and denary numbers is to write a subscript 2 at the end of a binary number and a subscript 10 at the end of a denary number. Thus

10012

is binary because of the subscript 2 at the end, whereas

100110

is denary because of the subscript 10 at the end.

Sometimes binary numbers are described as being ‘to base 2’. This fits with the subscript 2 at the end of the number. Similarly, denary numbers can be described as being ‘to base 10’, and this fits with the subscript 10 at the end.

You will meet the subscript convention on occasions in the rest of the unit.

Pages: <Previous  2    3    4    5    6    7    8    9    10    11    12  Next >