FaScan

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.

Value = bit0 x 20 + bit1 x 21 + bit2 x 22 + ... For example, if bits 0, 3 and 7 are enabled, the value is 1 + 8 + 128 = 137.
bit 727128
bit 62664
bit 52532
bit 42416
bit 3238
bit 2224
bit 1212
bit 0201

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.

TypeSizeTypical use
BYTE8 bitsSmall flag set or a value from 0 to 255.
WORD16 bitsOne Modbus register, status word, alarm word, control word.
DWORD32 bitsTwo 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 0123456789101112131415
part MANTISSA
content 1/211/221/231/241/251/261/271/281/291/2101/2111/2121/2131/2141/2151/216
bit value 0000000000000000
bit number 16171819202122232425262728293031
part MANTISSA EXPONENT S
content 1/2171/2181/2191/2201/2211/2221/2232021222324252627+/-
bit value 0000000000000000
Formula(-1)S x 1.M x 2E-127
MantissaThe fractional part. In a normalized REAL value, an implicit leading one is used: 1.M.
ExponentStored with a bias of 127. If the exponent field is 127, the real power is 0.
Sign0 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.

Practice For quick checks, use the bit calculator: choose a type, toggle bits or paste a HEX value from a register.