Overview
Number systems, or numeral systems, use a fixed set of symbols and a uniform rule set to represent numeric values. Every numeral system has two basic elements: base and place value.
Base and place value
Base: the number of distinct digits used by the system. For example, binary has a base of 2, decimal has a base of 10.
Place value: the value represented by a 1 in a given digit position. For example, in decimal 123, the place value of the 1 is 100, of the 2 is 10, and of the 3 is 1. In the binary number 1011, from left to right the place values are 8, 4, 2, and 1.
Common number systems used in PLCs
Common systems include decimal, binary, hexadecimal, and octal. BCD and ASCII are also occasionally used.
Decimal (Decimal notation)
An example: 1234 = 1×10^3 + 2×10^2 + 3×10^1 + 4×10^0. The base is 10, digits are 0–9, and each digit is multiplied by the base raised to the power of its position.
Binary (Binary notation)
An example: 1101 = 1×2^3 + 1×2^2 + 0×2^1 + 1×2^0 = 13. The base is 2, digits are 0 and 1. For signed numbers, the most significant bit is used as the sign bit, 0 for positive and 1 for negative. Positive numbers use the sign-magnitude or straightforward binary representation; negative numbers are commonly stored in two's complement, obtained by inverting the bits of the magnitude and adding 1.
Hexadecimal (Hexadecimal notation)
Base 16, using digits 0–9 and letters A–F (or a–f) to represent values 10–15.
Octal (Octal notation)
Base 8, digits 0–7. Octal is sometimes used for addressing in PLCs but is less common for arithmetic.
BCD (Binary-Coded Decimal)
BCD encodes each decimal digit 0–9 as a 4-bit binary group. BCD is often used to store clock values.
ASCII (American Standard Code for Information Interchange)
ASCII is a widely used encoding for Latin-based characters and common symbols. Because PLC data storage is binary, text and symbols are represented by agreed character codes such as ASCII.
Floating point (float)
Also called real numbers. A floating-point number represents a real value approximately as a significand multiplied by a base raised to an exponent. In binary scientific notation: S = M × 2^N. The IEEE 754 single-precision float uses 32 bits: 1 sign bit, 8 exponent bits, and 23 fraction bits.
Sign bit: 0 for positive, 1 for negative.
Exponent: stored using a bias. For single precision the bias is 127, so the stored exponent = actual exponent + 127. The 8-bit exponent can represent actual exponent values in approximately ?128 to +127.
Significand (mantissa): the fractional part. For normalized numbers, the integer part of M is implicitly 1 and is not stored.
Floating-point example
Convert 125.5 to IEEE 754 single-precision format. The integer 125 is 1111101 in binary. The fractional 0.5 is 0.1 in binary, so 125.5 = 1111101.1 in binary. Normalize to 1.1111011 × 2^6. The exponent is 6, so stored exponent = 6 + 127 = 133, which is 10000101 in binary. The fractional part (with the implicit leading 1 removed) is 1111011, padded with zeros to 23 bits: 11110110000000000000000. The full 32-bit representation is: 0 10000101 11110110000000000000000.
Conversion methods
Below are common conversion techniques between bases.
1. Decimal to binary
Use repeated division by 2, recording remainders. The remainders, read from last to first, form the binary representation.
2. Binary to decimal
Expand the binary number by place value and sum the results to get the decimal value.
3. Binary to octal
Group binary digits into sets of three, starting from the right. Convert each 3-bit group to its octal digit. If the leftmost group has fewer than three bits, pad with leading zeros.
4. Octal to binary
Convert each octal digit to a 3-bit binary group. If necessary, pad the leftmost group with leading zeros.
5. Binary to hexadecimal
Group binary digits into sets of four, starting from the right. Convert each 4-bit group to a hexadecimal digit. Pad the leftmost group with leading zeros if needed.
6. Hexadecimal to binary
Convert each hexadecimal digit to a 4-bit binary group. Pad the leftmost group with leading zeros if necessary.
7. Decimal to octal or hexadecimal
There are two approaches:
- Indirect method: convert decimal to binary first, then convert binary to octal or hexadecimal.
- Direct method: use repeated division by 8 or 16, recording remainders. Read remainders from last to first to form the target base representation.
8. Octal or hexadecimal to decimal
Expand the number by place value in the respective base and sum the results to obtain the decimal value.
9. BCD to decimal
BCD encodes each decimal digit as a 4-bit binary group. Convert each 4-bit group to its decimal digit to obtain the full decimal number. This is analogous to grouping binary digits for hexadecimal conversion.
The above summarizes common numeral systems used in PLCs and their conversion methods, to help practitioners understand numeral concepts and conversion rules. In practice, programmer calculators and software tools can perform these conversions quickly.