## Appendix Number systems

We are so used to the decimal number system that it is difficult to conceive of any other way of counting. Normal everyday arithmetic is based on multiples of ten; for example, the number 4057 means:

4 thousands = 4 x 10 x 10 x 10 = 4000

plus 0 hundreds = 0 x 10 x 10 = 000

plus 5 tens = 5 x 10 = 50

plus 7 units = 7 = 7

Total = 4057

Each position in a decimal number represents a power of ten. Our day-to-day calculations are performed to a base of ten because we have ten fingers, but counting can be done to any number base. Of particular interest are number bases of eight (called octal), sixteen (called hexadecimal or hex for short) and two (called binary). In the discussion below we will use the suffix o for an octal number, h for a hex number and d or text for a decimal number where there is any possibility of confusion. 124o is thus octal, 306h is hex and 255d or twelve are decimal.

Octal, to base eight, uses the digits 0–7. In octal you count 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14 and so on. The octal number 14o means one eight and four units, which is decimal 12d. Similarly 317o means:

3 x 8 x 8 = 192d

plus 1 x 8 = 8d

plus 7 = 7d

Total = 207d

Hex deals with numbers in multiples of sixteen. We thus need some way of writing a single digit to represent decimal numbers ten to fifteen. For these the capital letters A to F are used. In hex you count 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, 13, etc. The hex number 12h

is one sixteen plus two units which is decimal 18d. Similarly C52h means:

12 x 16 x 16 = 3072d (Ch is 12d) plus 5 ´ 16 = 90d

plus 2 = 2d

Total = 3164d

We will return to octal and hex shortly.

Binary, to base two, only needs two symbols, 0 and 1. Each position in a binary number represents a power of two and is called a bit (for binary digit). In binary you count 0, 1, 10, 11, 100, 101, 110, 111, etc. A binary number rapidly grows in length. A binary number such as 101101 is evaluated in exactly the same way as we saw earlier for decimal, octal and hex. Binary 101101 is:

1 x 2 x 2 x 2 x 2 x 2 = 32d

plus 0 x 2 x 2 x 2 x 2 = 0

plus 1 x 2 x 2 x 2 = 8d

plus 1 x 2 x 2 = 4d

plus 0 x 2 = 0

plus 1 = 1

Total = 45d

Similarly 1101011 is (noting 2 x 2 = 4d, 2 x 2 x 2 = 8d and so on):

1 x 64d = 64d

plus 1 x 32d = 32d

plus 0 x 16d = 0

plus 1 x 8d = 8d

plus 0 x 4d = 0

plus 1 x 2d = 2d

plus 1 = 1

Total = 107d

Conversion from decimal to binary is achieved by successive division by two, and noting the remainders. Reading the remainder from the top (LSB, least significant bit) to bottom (MSB, most significant bit) gives the binary equivalent. For example, 23d:

23

11 rl (LSB)

5 rl

2 rl

1 r0

0 rl (MSB)

Decimal 23 is thus binary 10111.

Binary numbers are used in computers and PLCs because the two states, 0 and 1, are easy to handle with simple circuits. Commonly, eight binary bits (called a byte) and sixteen binary bits (called a word) are used. A byte can represent a number from 0 to 255, and a sixteen-bit word a number from 0 to 65535.

Octal and hex give a simple way of representing binary numbers. To convert a given binary number to octal, the binary number is written in groups of three bits (from the LSB) and the octal representation written directly underneath. For example, 11010110:

giving 326o directly in octal.

Hex conversion is similar, but groupings of four are used. Taking the same binary number 11010110:

giving D6 in hex.

Octal 326o and hex D6h are both representations of the same binary number 11010110.

PLCs (like all computers) work internally in binary, but this is difficult for human beings to deal with. Octal and hex are therefore used in many places as a halfway house between the internal workings of the machine and our decimal system. Siemens, for example, use octal bytes, and Allen Bradley label I/O addresses in octal.

A single decimal digit can lie between 0 and 9 inclusive. Four binary bits are therefore needed to represent one decimal digit. Decimal displays and keypads frequently ignore bit combinations 1010 (10d) to 1111 (15d) giving binary coded decimal or BCD. In BCD, each *decade *is coded independently into binary. For example:

BCD is not as efficient as pure binary. Twelve bits can represent 0–4095 in binary, but only 0–999 in BCD. It is, however, much easier to inter- face to external devices.

Binary arithmetic is similar to decimal arithmetic. Consider the decimal sum:

This is evaluated in three steps:

(a) 5 + 2 = 7, no carry

(b) 4 + 7 = 11, one down (as result) plus carry

(c) 3 + 2 + carry = 6

At each stage we consider three ‘inputs’, the two digits to be added and a possible carry from the previous (lesser significant) column. Each column has two outputs; a result and a carry to the next column.

Binary addition is similar, except there are only two possible states for each digit and the carry, allowing us to build a simple truth table with just eight entries (Table A.1).

An example of binary arithmetic is:

The implementation of an adder truth table is a simple problem of com- binational logic.

Negative numbers are generally represented in a form called two’s complement. The most significant digit represents the sign, being 0 for positive numbers and 1 for negative numbers. The value part of a negative number is complemented (1s changed to 0s and vice versa) and 1 added. For example, +12d in 8-bit binary two’s complement is 00001100 and -12d is 11110100.

As expected, addition of a positive and negative number of the same value will give a result of zero:

The top ninth bit is lost with an 8-bit byte, giving the expected result of zero.

Two’s complement therefore allows subtraction to be performed with simple addition. For example, 12d – 3d:

The top bit is again lost giving the correct result of +9d. An 8-bit byte in two’s complement form can represent -128d to +127d, and a 16-bit word from -32768d to +32767d. Integers inside a PLC are generally held in 16-bit two’s complement form.