The term "number system" refers to the method of representing numbers, i.e., the way of counting. A number system uses a method of carrying, where when the value increases to the maximum representable by a digit, an additional digit is added (carry). A number system that counts according to the rules of carrying is called a positional number system. Common positional number systems include Binary, Decimal, and Hexadecimal.
Human thinking is accustomed to the Decimal system, where counting advances by one after reaching ten. This is a well-known concept for Earthlings.
In the context of PLC programming, we will focus on Binary and Hexadecimal.
Modern computers generally adopt the architecture proposed by John von Neumann. Based on the bistable characteristics of electronic components, John von Neumann suggested using the Binary system for digital computers.
Binary, with its two digits "0" and "1", can represent the two different states of a numeric quantity, such as the closure and opening of a contact or the energized and de-energized states of a coil. In PLC memory units, a "bit" can be used to represent either "0" or "1", and eight bits make up a byte. Taking Siemens S7 series PLC as an example, you can use the STL statement "S Q0.0" to set Q0.0 to "1". In this case, "Q0.0" represents the Output Process Image Area, the first "0" represents the 0th byte, and the second "0" represents the 0th bit of that byte. When this instruction is executed, the output module's Q0.0 will have a signal output, and the connected relay coil will be energized, closing the corresponding contacts.
Binary follows the "carry by two" rule, and multiple binary digits can be used to represent numerical values. The lowest bit is labeled as the 0th bit, and the nth bit from right to left has a weight (the value represented by a 1 in that position) of 2 to the power of n.
In the Siemens S7 series PLC, binary constants start with "2#", for example: 2#1101. Binary numbers can be converted to decimal numbers by multiplying each digit by its weight and then adding them up. For example, 2#1101 is equal to 1 times 2 to the power of 3 (=8), plus 1 times 2 to the power of 2 (=4), plus 1 times 2 to the power of 0 (=1), resulting in the decimal value 13.
Due to the inconvenience of writing binary numbers with too many digits, people group four binary digits together to represent a number. As four binary digits can represent a maximum value of 2#1111 (=15), the hexadecimal number system emerged. The hexadecimal system includes digits from 0 to 9 and letters A to F, where A is equivalent to decimal 10 and F is equivalent to decimal 15.
There are various ways to represent hexadecimal numbers: in C/C++, it is represented with the prefix "0x," as mentioned in the previous article ("0x0384"); in VB, it uses the prefix "&H" (e.g., "&H1A2B"); in Siemens S7 series PLC, "16#" is used to represent hexadecimal numbers. However, in most cases, when representing hexadecimal numbers, it is necessary to indicate their data type (there will be a dedicated article on data types later). For example, B#16#56 represents the hexadecimal number 56, and its data type is Byte.
Hexadecimal numbers can be easily converted to binary numbers by converting each digit to binary and then combining them. For instance, B#16#56, where 16#5=2#0101 and 16#6=2#0110, so 16#56=2#1010110, as shown below:
(Binary-Coded Decimal)is another encoding used in program design. BCD is the abbreviation for "Binary-Coded Decimal," and this coding system is based on the decimal number system. BCD uses four binary digits to represent one decimal digit, and each digit is allowed to range from 2#0000 to 2#1001, equivalent to decimal 0 to 9, following the "carry-ten" rule.
BCD code is signed, using the highest bit to represent the sign. "0" indicates a positive number, while "1" indicates a negative number. For a four-digit BCD code (16 binary bits), the range it can represent is "-999 to +999." Why is that? It's quite simple to understand. Considering the highest bit as the sign bit, we are left with three digits. Since each BCD digit is essentially a decimal digit, and the maximum value for each digit is 9, the maximum three-digit BCD number is 999. Adding the sign, a four-digit BCD code can represent decimal numbers in the range of "-999 to +999." Similarly, an eight-digit BCD code (32 binary bits) can represent decimal numbers in the range of "-9999999 to +9999999."
Alright, that covers the knowledge about number systems needed for PLC programming.