Binary arithmetic

Introduction

An important part of the use of logic circuits is for computing various

 mathematical operations such as addition, multiplication, trigonometric

 operations, etc.

We must therefore have a way of representing numbers as binary data.

Binary arithmetic

Arithmetic in binary is much like arithmetic in other numeral systems. Addition, subtraction, multiplication, and division can be performed on binary numerals.

Addition:

The simplest arithmetic operation in binary is addition. Adding two single-digit
 binary numbers is relatively simple:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10 (carry:1)

Adding two "1" values produces the value "10" (spoken as "one-zero"),

equivalent to the decimal value 2. This is similar to what happens

in decimal when certain single-digit numbers are added together;

if the result equals or exceeds the value of the radix (10),

 the digit to the left is incremented:

5 + 5 = 10
7 + 9 = 16

This is known as carrying in most numeral systems. When the result

 of an addition exceeds the value of the radix, the procedure is to

 "carry the one" to the left, adding it to the next positional value.

 Carrying works the same way in binary:

1 1 1 1 1 (carried digits) 0 1 1 0 1 + 1 0 1 1 1
 ------------- = 1 0 0 1 0 0

In this example, two numerals are being added together: 011012

(13 decimal) and 101112 (23 decimal).

The top row shows the carry bits used. Starting in

the rightmost column, 1 + 1 = 102. The 1 is carried to the left,

 and the 0 is written at the bottom of the rightmost column.

The second column from the right is added: 1 + 0 + 1 = 102

again; the 1 is carried, and 0 is written at the bottom.

The third column: 1 + 1 + 1 = 112. This time, a 1 is carried,

 and a 1 is written in the bottom row. Proceeding like

this gives the final answer 1001002 (36 decimal).

Subtraction:

Subtraction works in much the same way:

0 - 0 = 0
0 - 1 = 1 (with borrow)
1 - 0 = 1
1 - 1 = 0

One binary numeral can be subtracted from another as follows:

* * * * (starred columns are borrowed from)
 1 1 0 1 1 1 0 - 1 0 1 1 1 ---------------- = 1 0 1 0 1 1 1

Subtracting a positive number is equivalent to adding a negative number of equal absolute value; computers typically use tow's complement notation to represent negative values. This notation eliminates the need for a separate "subtract" operation. The subtraction can be summarized with this formula:

A - B = A + not B + 1

For further details, see tow's complement.

Multiplication

Multiplication in binary is similar to its decimal counterpart. Two numbers A and B can be multiplied by partial products: for each digit in B, the product of that digit in A is calculated and written on a new line, shifted leftward so that its rightmost digit lines up with the digit in B that was used. The sum of all these partial products gives the final result.

Since there are only two digits in binary, there are only two possible outcomes of each partial multiplication:

  • If the digit in B is 0, the partial product is also 0
  • If the digit in B is 1, the partial product is equal to A

For example, the binary numbers 1011 and 1010 are multiplied as follows:

 1 0 1 1 (A) × 1 0 1 0 (B) --------- 0 0 0 0 ? Corresponds
 to a zero
 in B + 1 0 1 1 ? Corresponds to a one in 
B + 0 0 0 0 + 1 0 1 1 
--------------- = 1 1 0 1 1 1 0

 Division

Binary division is again similar to its decimal counterpart:

 __________ 1 0 1 | 1 1 0 1 1

Here, the divisor is 1012, or 5 decimal, while the dividend is 110112, or 27 decimal. The procedure is the same as that of decimal long division; here, the divisor 1012

goes into the first three digits 1102 of the dividend one time, so a "1" is written on the

 top line. This result is multiplied by the divisor, and subtracted from the first three digits of the dividend; the next digit (a "1") is included to obtain a new three-digit sequence:

 1 __________ 1 0 1 | 1 1 0 1 1 ? 1 0 1 ----- 0 1 1

The procedure is then repeated with the new sequence, continuing until the digits in the dividend have been exhausted:

 1 0 1 __________ 1 0 1 | 1 1 0 1 1 ? 1 0 1 ----- 0 1 1 
- 0 0 0 
----- 1 1 1 ? 1 0 1 ----- 1 0

Thus, the quotient of 110112 divided by 1012 is 1012, as shown on the top line, while the remainder, shown on the bottom line, is 102. In decimal, 27 divided by 5 is 5, with

a remainder of 2.