Back Home Up Next

Numbers Fractions Negative numbers Floating point Binary arithmetic Codes

Binary Arithmetic

 

The rules of binary arithmetic are identical to the rules of decimal arithmetic—decimal arithmetic uses the base ten and has ten digits, whereas binary arithmetic uses the base two and has two digits. The addition, subtraction and multiplication tables for binary arithmetic are given below and are much simpler than their decimal counterparts.

 

Addition                       Subtraction                              Multiplication

 

0 + 0 = 0                      0 - 0 = 0                                0 x 0 = 0

0 + 1 = 1                      0 - 1 = 1 (borrow 1)                     0 x 1 = 0

1 + 0 = 1                      1 - 0 = 1                                1 x 0 = 0

1 + 1 = 0 (carry 1)            1 - 1 = 0                                1 x 1 = 1

 

These tables define what happens when you add, subtract, or multiply two single-bit binary values. Real computers use 8-, 16-, 32-, and 64-bit numbers and, therefore, arithmetic operations must be applied to all the bits of a word. Consider first the decimal addition 263 + 482 = 715. We start at the least-significant columns and add 3 + 2 to get 5. Then we add the digits in the next column, 6 + 8, to get 4, carry 1. Finally, we add the two digits in the most-significant column, 2 + 4 plus the carry in, to get 7.

 

When you add two binary words, you have to add pairs of bits, a column at a time, starting with the least-significant bit. All you need remember is that any carry-out has to be added to the next column on the left. Consider the following four examples of 8-bit binary addition.

  


00101010           10011111             00110011             01110011

01001101           00000001             11001100             01110011

01110111           10100000             11111111             11100110

 

When subtracting two binary numbers, you have to remember that 0 - 1 results in the difference 1 and a borrow from the column on the left. Consider the following examples of binary subtraction.

  


 

01101001           10011111             10111011             10110000

01001001           01000001             10000100             01100011

00100000           01011110             00110111             01001101

 

 

Decimal multiplication is difficult—you have to learn the multiplication tables from 1 x 1 = 1, to 9 x 9 = 81. Binary multiplication is much easier because there is one simple multiplication table:

 

0 x 0 = 0

0 x 1 = 0

1 x 0 = 0

1 x 1 = 1

 

The following example demonstrates the multiplication of 011010012 (the multiplier) by 010010012 (the multiplicand). Note that the product of two n-bit words is a 2n-bit value. The technique used is identical to the long multiplication algorithm we are taught in high-school. You start with the least-significant bit of the multiplier and test whether it is a 0 or a 1. If it is a zero, you write down n zeros; if it is a 1 you write down the multiplier (this value is called a partial product). You then test the next bit of the multiplicand to the left and carry out the same operation—in this case you write zero or the multiplier one place to the left (i.e., the partial product is shifted left). The process is continued until you have examined each bit of the multiplicand in turn. Finally you add together the n partial products to generate the product of the multiplier and the multiplicand.

 

Multiplicand

Multiplier

Step

Partial product

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

01001001

01101001

1

 

 

 

 

 

 

 

0

1

1

0

1

0

0

1

01001001

01101001

2

 

 

 

 

 

 

0

0

0

0

0

0

0

0

 

01001001

01101001

3

 

 

 

 

 

0

0

0

0

0

0

0

0

 

 

01001001

01101001

4

 

 

 

 

0

1

1

0

1

0

0

1

 

 

 

01001001

01101001

5

 

 

 

0

0

0

0

0

0

0

0

 

 

 

 

01001001

01101001

6

 

 

0

0

0

0

0

0

0

0

 

 

 

 

 

01001001

01101001

7

 

0

1

1

0

1

0

0

1

 

 

 

 

 

 

01001001

01101001

8

0

0

0

0

0

0

0

0

 

 

 

 

 

 

 

 

 

Result

0

0

1

1

1

0

1

1

1

1

1

0

0

0

1

 

Let’s check this result. If we convert the two binary numbers to decimal form, multiply them and convert the result back to binary, we should get the same answer. First, we convert the multiplier to decimal form by adding up the powers of two to get 011010012 = 1 + 8 + 32 + 64 = 10510. Then we convert the multiplicand in the same way to get 010010012 = 1 + 8 + 64 = 7310. The product of these two decimal numbers is 105 x 73 = 7665. Finally, we convert this decimal value into binary form:

 

7665   ¸ 2 = 3832            R = 1

3832   ¸ 2 = 1916            R = 0

1916   ¸ 2 =  958            R = 0

 958   ¸ 2 =  479            R = 0

 479   ¸ 2 =  239            R = 1

 239   ¸ 2 =  119            R = 1

 119   ¸ 2 =   59            R = 1

  59   ¸ 2 =   29            R = 1

  29   ¸ 2 =   14            R = 1

  14   ¸ 2 =    7            R = 0

   7   ¸ 2 =    3            R = 1

   3   ¸ 2 =    1            R = 1

   1   ¸ 2 =    0            R = 1

 

The binary equivalent of 766510 is 11101111100012, which is the same value we calculated by directly multiplying the two binary numbers. Digital computer don’t multiply numbers in exactly the way we have just done. They employ various techniques to speed up the process (but the basic principles are the same).

Hexadecimal Arithmetic

 

Programmers find binary numbers tedious to handle because they are so long. Most texts covering computer hardware, architecture, and assembly language programming employ hexadecimal arithmetic rather than binary arithmetic. Hexadecimal, or base 16, arithmetic is popular because people find it so much easier to handle hexadecimal numbers than binary numbers, and because it is easy to convert hexadecimal values to binary values.

 

 

Decimal value

Hexadecimal digit

Binary Equivalent

 

 

 

0

0

0000

1

1

0001

2

2

0010

3

3

0011

4

4

0100

5

5

0101

6

6

0110

7

7

0111

8

8

1000

9

9

1001

10

A

1010

11

B

1011

12

C

1100

13

D

1101

14

E

1110

15

F

1111

Converting a hexadecimal digit into binary form

The weightings of the columns in the hexadecimal system are 1, 16, 265, 4096... (i.e., 160, 161, 162, 163...); for example, 12316 is equal to 1 x 162 + 2 x 161 + 3 x 160 = 256 + 32 + 3 = 27410. Table 1.2 gives the 16 hexadecimal digits. Note that we have to use letters for digits after 9. The hexadecimal value A1C616 is equal to 10 x 163 + 1 x 162 + 12 x 161 + 6 x 160 = 40960 + 256 + 192 + 6 = 4141410.

 

 

Hexadecimal arithmetic follows the same rules as both decimal and binary arithmetic. Adding together two hexadecimal digits, generates a carry out if the sum is 16 or greater; for example, 5 + 6 = B, whereas 9 + 9 = 2 carry 1 (the carry is, of course 16). Consider the three hexadecimal additions:

  
 

1234       A14C        BACE

4567       23B2        1A34

579B       C4FE        D502

 

 

All you have to remember when performing hexadecimal arithmetic is that A16 = 1010, B16 = 1110, C16 = 1210, D16 = 1310, E16 = 1410, and F16 = 1510. When two digits are added and their sum is 1610 or more, you have to carry the 16 to the next column and write down the remainder (just as you would do with decimal numbers). For example, 2916 + 3816 = 6116.

 

The hexadecimal base is so appealing because it is easy to convert between hexadecimal and binary values as table 1.2 demonstrates. To convert a multi-digit hexadecimal number into binary form all you do is write down each hexadecimal digit as the corresponding four binary bits; for example, A012CF16 = 1010  0000  0001  0010  1100  1111 (as groups of 4 bits) = 1010000000010010110011112. As you can see, the six-digit hexadecimal number is much easier to write down than the 24-bit binary string. The inverse operation, binary to hexadecimal conversion, is performed by dividing the binary bits into groups of four, starting at the right-hand end of the number and then writing down the corresponding hexadecimal digit; for example: 11000011101010012 = 1100  0011  1010  1001 = C3A916.