# Binary Arithmetic - Programmable Logic controllers

Addition of binary numbers uses the following rules:

Consider the addition of the binary numbers 01110 and 10011.

For bit 0 in the sum, 0 þ 1 ¼ 1. For bit 1 in the sum, 1 þ 1 ¼ 10, and so we have 0 with 1 carried to the next column. For bit 2 in the sum, 1 þ 0 þ the carried 1 ¼ 10.For bit 3 in the sum, 1 þ 0 þ the carried 1 ¼ 10. We continue this process through the various bits and end up with 100001.

Subtraction of binary numbers follows these rules:

When evaluating 0 À 1, a 1 is borrowed from the next column on the left that contains a 1. The following example illustrates this method with the subtraction of 01110 from 11011:

For bit 0 we have 1 À 0 ¼ 1. For bit 1 we have 1 À 1 ¼ 0. For bit 2 we have 0 À 1. We thus borrow 1 from the next column and so have 10 À 1 ¼ 1. For bit 3 we have 0 À 1 (remember,we borrowed the 1). Again borrowing 1 from the next column, we then have 10 À 1 ¼ 1. Forbit 4 we have 0 À 0 ¼ 0 (remember, we borrowed the 1).

Signed Numbers

The binary numbers considered so far contain no indication as to whether they are negative or positive and are thus said to be unsigned. Since there is generally a need to handle both positive and negative numbers, there needs to be some way of distinguishing between them.

This can be done by adding a sign bit. When a number is said to be signed, its MSB is used to indicate the sign of the number; a 0 is used if the number is positive and a 1 is used if it is negative. Thus for an 8-bit number we have:

When we have a positive number, we write it in the normal way, with a 0 preceding it. Thus a positive binary number of 10110 is written as 010110. A negative number of 10110 is written as 110110. However, this is not the most useful way of writing negative numbers for ease of manipulation by computers.

One’s and Two’s Complements

A more useful way of writing signed negative numbers is to use the two’s complement method. A binary number has two complements, known as the one’s complement and the two’s complement. The one’s complement of a binary number is obtained by changing all the 1s in the unsigned number into 0s and the 0s into 1s. Thus if we have the binary number 101101, the one’s complement of it is 010010. The two’s complement is obtained from the one’s complement by adding 1 to the LSB of the one’s complement. Thus the two’s complement of 101101 becomes 010011.

When we have a negative number, to obtain the signed two’s complement, we obtain the two’s complement and then sign it with a 1. Consider the representation of the decimal number À6 as a signed two’s complement number when the total number of bits is eight. We first write the binary number for þ6, that is, 0000110, then obtain the one’s complement of 1111001, add 1 to give 1111010, and finally sign it with a 1 to indicate it is negative. The result is thus 11111010.

Table 3.2 lists some signed two’s complements for denary numbers, given to 4 bits.

When we have a positive number, we sign the normal binary number with a 0, that is, we write only negative numbers in the two’s complement form. A consequence of adopting this method of writing negative and positive numbers is that when we add the signed binary equivalent of þ4 and –4, that is, 0000 0100 and 111 1100, we obtain (1)0000 0000 and so zero within the constraints of the number of bits used, the (1) being neglected.

Subtraction of a positive number from a positive number can be considered to be the addition of a negative number to a positive number. Thus we obtain the signed two’s complement of the negative number and then add it to the signed positive number. Hence, for the subtraction of the denary number 6 from the denary number 4, we can consider the problem as being(þ4) þ (À6). Hence we add the signed positive number to the signed two’s complement for the negative number.

The MSB, that is, the sign, of the outcome is 1 and so the result is negative. This is the 8-bit signed two’s complement for À2.

If we wanted to add two negative numbers, we would obtain the signed two’s complement for each number and then add them. Whenever a number is negative, we use the signed two’s complement; when it’s positive, we use just the signed number.

Floating Point Numbers

Before we discuss floating point numbers, let’s consider fixed point numbers. Fixed point numbers are numbers for which there is a fixed location of the point separating integers from fractional numbers. Thus, 15.3 is an example of a denary fixed point number, 1010.1100 an example of a fixed point binary number, and DE.2A an example of a fixed point hexadecimal number. We have, with the 8-bit binary number, four digits before the binary point and four digits after it. When two such binary numbers are added by a computing system, the procedure is to recognize that the fixed point is fixed the same in both numbers, so we can ignore it for the addition, carry out the addition of the numbers, and then insert inthe result the binary point in its fixed position. For example, suppose we want to add 0011.1010 and 0110.1000; we drop the binary point to give:

0011 1010 þ 0110 1000 ¼ 1010 0010

Inserting the binary point then gives 1010.0010.

Using fixed points does present problems. If we are concerned with very large or very small numbers, we could end up with a large number of zeros between the integers and the point,that is, 0.000 000 000 000 023. For this reason, scientific notation is used for such numbers. Thus, the above number might be written as 0.23 Â 10–13 or 2.3 Â 10–14 or 23 Â 10–15. Likewise, the binary number 0.0000 0111 0010 might be represented as 110010 Â 2–12(the 12 would also be in binary format) or 11001.0 Â 2–11 (the 11 being in binary format). Such notation is said to have a floating point.

A floating point number is in the form a Â re, where a is termed the mantissa, r the radixor base, and e the exponent or power. With binary numbers the base is understood to be 2,that is, we have a Â 2e, and when we know we are dealing with binary numbers we need not store the base with the number. Thus a computing system needs, in addition to storing the sign, that is, whether positive or negative, to store the mantissa and the exponent.

Because with floating point numbers it is possible to store a number in several different ways—for example, 0.1 Â 102 and 0.01 Â 103—with computing systems such numbers are normalized. This means that they are all put in the form 0.1 Â re. Thus, with binary numbers we have 0.1 Â 2e; if we had 0.00001001 it would become 0.1001 Â 2–4. To take account ofthe sign of a binary number, we then add a sign bit of 0 for a positive number and 1 for a negative number. Thus the number 0.1001 Â 2–4 becomes 1.1001 Â 2–4 if negative and 0.1001 Â 2–4 if positive.

Unlike fixed point numbers, floating point numbers cannot be directly added unless the exponents are the same. Thus to carry out addition we need to make the exponents the same.