Knowledge base
Bit-level variable representation
A variable can store not only a regular number, but also a set of separate flags. Industrial automation often packs states, alarms, permissions, operating modes and status flags into Modbus registers this way.
A bit as a separate flag
Each bit has a number and a weight. If the bit is 1, its weight is added to the final value. If the bit is 0, it adds nothing. For integer types this is a simple sum of powers of two.
BYTE, WORD and DWORD
BYTE contains 8 bits, WORD contains 16 bits, and DWORD contains 32 bits. In Modbus maps a 16-bit holding register is often described as a WORD: it can represent one value from 0 to 65535 or 16 independent flags.
| Type | Size | Typical use |
|---|---|---|
BYTE | 8 bits | Small flag set or a value from 0 to 255. |
WORD | 16 bits | One Modbus register, status word, alarm word, control word. |
DWORD | 32 bits | Two Modbus registers, a larger flag mask or a 32-bit value. |
Signed types
In SHORT8, INT16 and DINT32, the highest bit participates in signed representation. Two's complement is typically used: if the highest bit is set, the number is negative. For example, an INT16 bit pattern 1111 1111 1111 1111 means -1, not 65535.
REAL: sign, exponent and mantissa
REAL is not a simple sum of bits. In most PLC and SCADA systems it is stored as a 32-bit IEEE-754 Float32 value: one sign bit, 8 exponent bits and 23 mantissa bits. The same 32 bits can be interpreted as UDINT32 or as REAL, but the meaning is different.
| bit number | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| part | MANTISSA | |||||||||||||||
| content | 1/21 | 1/22 | 1/23 | 1/24 | 1/25 | 1/26 | 1/27 | 1/28 | 1/29 | 1/210 | 1/211 | 1/212 | 1/213 | 1/214 | 1/215 | 1/216 |
| bit value | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| bit number | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| part | MANTISSA | EXPONENT | S | |||||||||||||
| content | 1/217 | 1/218 | 1/219 | 1/220 | 1/221 | 1/222 | 1/223 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | +/- |
| bit value | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
(-1)S x 1.M x 2E-1271.M.0 means positive, 1 means negative.What matters in Modbus
Modbus transfers 16-bit registers. If a variable occupies 32 bits, it is stored in two registers. For DWORD, DINT32 and REAL, word order matters: high word first or low word first. If the order is wrong, the bits are the same, but the decoded value is wrong.