Binary numbers are the most fundamental numbering system in all computers. A single binary digit (a bit) corresponds to the condition of a single wire. If the voltage on the wire is true the bit value is 1. If the voltage is off the bit value is 0. If two or more wires are used then each new wire adds another significant digit. Each binary number will have an equivalent digital value. Figure 12.1 Conversion of a Binary Number to a Decimal Number shows how to convert a binary number to a decimal equivalent. Consider the digits, starting at the right. The least significant digit is 1, and is in the 0th position. To convert this to a decimal equivalent the number base (2) is raised to the position of the digit, and multiplied by the digit. In this case the least significant digit is a trivial conversion. Consider the most significant digit, with a value of 1 in the 6th position. This is converted by the number base to the exponent 6 and multiplying by the digit value of 1. This method can also be used for converting the other number system to decimal.
Figure 12.1 Conversion of a Binary Number to a Decimal Number
Decimal numbers can be converted to binary numbers using division, as shown in Figure 12.1 Conversion from Decimal to Binary. This technique begins by dividing the decimal number by the base of the new number. The fraction after the decimal gives the least significant digit of the new number when it is multiplied by the number base. The whole part of the number is now divided again. This process continues until the whole number is zero. This method will also work for conversion to other number bases.
Figure 12.1 Conversion from Decimal to Binary
Most scientific calculators will convert between number bases. But, it is important to understand the conversions between number bases. And, when used frequently enough the conversions can be done in your head.
Binary numbers come in three basic forms - a bit, a byte and a word. A bit is a single binary digit, a byte is eight binary digits, and a word is 16 digits. Words and bytes are shown in Figure 12.1 Bytes and Words. Notice that on both numbers the least significant digit is on the right hand side of the numbers. And, in the word there are two bytes, and the right hand one is the least significant byte.
Binary numbers can also represent fractions, as shown in Figure 12.1 A Binary Decimal Number. The conversion to and from binary is identical to the previous techniques, except that for values to the right of the decimal the equivalents are fractions.
Figure 12.1 A Binary Decimal Number
In the next chapter you will learn that entire blocks of inputs and outputs can be used as a single binary number (typically a word). Each bit of the number would correspond to an output or input as shown in Figure 12.1 Motor Outputs Represented with a Binary Number.
Figure 12.1 Motor Outputs Represented with a Binary Number
We can then manipulate the inputs or outputs using Boolean operations. Boolean algebra has been discussed before for variables with single values, but it is the same for multiple bits. Common operations that use multiple bits in numbers are shown in Figure 12.1 Boolean Operations on Binary Numbers. These operations compare only one bit at a time in the number, except the shift instructions that move all the bits one place left or right.
Negative numbers are a particular problem with binary numbers. As a result there are three common numbering systems used as shown in Figure 12.1 Binary (Integer) Number Types. Unsigned binary numbers are common, but they can only be used for positive values. Both signed and 2s compliment numbers allow positive and negative values, but the maximum positive values is reduced by half. 2s compliment numbers are very popular because the hardware and software to add and subtract is simpler and faster. All three types of numbers will be found in PLCs.
Figure 12.1 Binary (Integer) Number Types
Examples of signed binary numbers are shown in Figure 12.1 Signed Binary Numbers. These numbers use the most significant bit to indicate when a number is negative.
Figure 12.1 Signed Binary Numbers
An example of 2s compliment numbers are shown in Figure 12.1 2s Compliment Numbers. Basically, if the number is positive, it will be a regular binary number. If the number is to be negative, we start the positive number, compliment it (reverse all the bits), then add 1. Basically when these numbers are negative, then the most significant bit is set. To convert from a negative 2s compliment number, subtract 1, and then invert the number.
Figure 12.1 2s Compliment Numbers
Using 2s compliments for negative numbers eliminates the redundant zeros of signed binaries, and makes the hardware and software easier to implement. As a result most of the integer operations in a PLC will do addition and subtraction using 2s compliment numbers. When adding 2s compliment numbers, we don’t need to pay special attention to negative values. And, if we want to subtract one number from another, we apply the twos compliment to the value to be subtracted, and then apply it to the other value.
Figure 12.1 Adding 2s Compliment Numbers shows the addition of numbers using 2s compliment numbers. The three operations result in zero, positive and negative values. Notice that in all three operation the top number is positive, while the bottom operation is negative (this is easy to see because the MSB of the numbers is set). All three of the additions are using bytes, this is important for considering the results of the calculations. In the left and right hand calculations the additions result in a 9th bit - when dealing with 8 bit numbers we call this bit the carry C. If the calculation started with a positive and negative value, and ended up with a carry bit, there is no problem, and the carry bit should be ignored. If doing the calculation on a calculator you will see the carry bit, but when using a PLC you must look elsewhere to find it.
Figure 12.1 Adding 2s Compliment Numbers
The integers have limited value ranges, for example a 16 bit word ranges from -32,768 to 32,767 whereas a 32 bit word ranges from -2,147,483,648 to 2,147,483,647. In some cases calculations will give results outside this range, and the Overflow O bit will be set. (Note: an overflow condition is a major error, and the PLC will probably halt when this happens.) For an addition operation the Overflow bit will be set when the sign of both numbers is the same, but the sign of the result is opposite. When the signs of the numbers are opposite an overflow cannot occur. This can be seen in Figure 12.1 Carry and Overflow Bits where the numbers two of the three calculations are outside the range. When this happens the result goes from positive to negative, or the other way.
Figure 12.1 Carry and Overflow Bits
These bits also apply to multiplication and division operations. In addition the PLC will also have bits to indicate when the result of an operation is zero Z and negative N.
Other number bases are typically converted to and from binary for storage and mathematical operations. Hexadecimal numbers are popular for representing binary values because they are quite compact compared to binary. (Note: large binary numbers with a long string of 1s and 0s are next to impossible to read.) Octal numbers are also popular for inputs and outputs because they work in counts of eight; inputs and outputs are in counts of eight.
An example of conversion to, and from, hexadecimal is shown in Figure 12.1 Conversion of a Hexadecimal Number to a Decimal Number and Figure 12.1 Conversion from Decimal to Hexadecimal. Note that both of these conversions are identical to the methods used for binary numbers, and the same techniques extend to octal numbers also.
Figure 12.1 Conversion of a Hexadecimal Number to a Decimal Number
Figure 12.1 Conversion from Decimal to Hexadecimal
Binary Coded Decimal (BCD) numbers use four binary bits (a nibble) for each digit. (Note: this is not a base number system, but it only represents decimal digits.) This means that one byte can hold two digits from 00 to 99, whereas in binary it could hold from 0 to 255. A separate bit must be assigned for negative numbers. This method is very popular when numbers are to be output or input to the computer. An example of a BCD number is shown in Figure 12.1 A BCD Encoded Number. In the example there are four digits, therefore 16 bits are required. Note that the most significant digit and bits are both on the left hand side. The BCD number is the binary equivalent of each digit.
Figure 12.1 A BCD Encoded Number
Most PLCs store BCD numbers in words, allowing values between 0000 and 9999. They also provide functions to convert to and from BCD. It is also possible to calculations with BCD numbers, but this is uncommon, and when necessary most PLCs have functions to do the calculations. But, when doing calculations you should probably avoid BCD and use integer mathematics instead. Try to be aware when your numbers are BCD values and convert them to integer or binary value before doing any calculations.