2012-05-17

Binary and decimal

In a recent post, I described briefly how the binary numeric format fits well with the octal and hexadecimal formats.  However, we humans mainly use the decimal format to make calculations and express quantities.  Say we want a binary computer to actually work in decimal, storing quantities as decimal digits and using decimal calculations.  Is this possible?

Representing

To begin with, a decimal digit can be represented in binary using four bits:
Binary      Decimal
 0000          0
 0001          1
 0010          2
 0011          3
 0100          4
 0101          5
 0110          6
 0111          7
 1000          8
 1001          9 
This means that 1138 can be stored as1
 0001 0001 0011 1000
One unavoidable problem with this representation  is extra values: four bits can represent 16 different values, and we only have 10 digits to represent.  We can make use of a couple of these values to represent plus and minus signs, which are good to have around when representing numbers.  Hexadecimal C becomes + (for "Credit") and D becomes - (for "Debit").2
Binary      Hexadecimal     Meaning
 1010            A
 1011            B
 1100            C             +
 1101            D             -
 1110            E
 1111            F 
The sign is typically stored at the end of the number, in contrast to the way we write signed numbers on paper.  Another convention is to have an even number of fields in the representation, adding a 0000 field in front if necessary.  +1138 is stored like this:
 0000 0001 0001 0011 1000 1100
        1    1    3    8    + 

Processing

Now, can we add two positive numbers, say +1138 and +199?
   0000 0001 0001 0011 1000    1138
 + 0000 0000 0001 1001 1001  +  199
--------------------------- -------
Just as with paper calculations, we'll work with one digit position at a time, starting from the right.  Our binary processor knows how to add 1001 (9) and 1000 (8) getting 10001 (17).  This is not one of our ten legal digit values.  There is a simple fix, though.  Whenever the 5-bit sum of two digits is an illegal value, add six3 and carry the excess.  The new result becomes 10111.  The leftmost bit becomes a carried bit which will be a part of the next calculation.
                     1           1
   0000 0001 0001 0011 1000    1138
 + 0000 0000 0001 1001 1001  +  199
--------------------------- -------
                       0111       7
The tens digit is produced by a three-way addition: one digit from each number and one bit from carry.  Binary addition of 0001, 1001, and 0011 produces the sum 01101; adding six gives us 10011.
                1    1          11
   0000 0001 0001 0011 1000    1138
 + 0000 0000 0001 1001 1001  +  199
--------------------------- -------
                  0011 0111      37
The hundreds digit is calculated to be 00011, which is a legal digit value.  The thousands digit becomes 00001.
                1    1          11
   0000 0001 0001 0011 1000    1138
 + 0000 0000 0001 1001 1001  +  199
--------------------------- -------
   0000 0001 0011 0011 0111    1337
Other kinds of processing, such as subtraction or multiplication, can be performed in a similar manner.

And the point is...?

This kind of decimal processing on a binary computer, known as Binary Coded Decimal (BCD), was popular during the early decades of computing when binary calculations on computers were still limited and somewhat inexact.  BCD still has some advantages in that it's more closely coupled with the way humans use numbers (and how numbers are printed on a screen or on paper), but modern binary processing is a lot faster and more size-effective.  Software used in financial systems are still likely to use BCD, though.

1) The normal binary representation of 1138 is 0100 0111 0010.
2) Yes, that is the traditional derivation of the meanings; decimal encoding has its roots in financial computing.
3) Because there are 16 possible values in four bits - 10 desired values = 6.

No comments:

Post a Comment