Skip to content

Bit Operations and Masks

Bit operations are useful when a device, telegram, status word, or diagnostic value packs many Boolean signals into one integer-like value.

Words As Bit Fields

A Word or DWord can be treated as a set of bits. Each bit has a value:

Bit Hex mask
0 16#0001
1 16#0002
2 16#0004
3 16#0008
4 16#0010
5 16#0020
6 16#0040
7 16#0080

The mask selects the bit you care about.

Check Whether A Bit Is Set

xboDriveReady := (woStatusWord AND 16#0001) <> 0;
xboDriveFault := (woStatusWord AND 16#0008) <> 0;

The AND operation clears every bit except the selected bit. If the result is not zero, that bit was set.

Set A Bit

woCommandWord := woCommandWord OR 16#0001;

OR keeps all existing bits and forces the selected bit to 1.

Clear A Bit

woCommandWord := woCommandWord AND NOT 16#0001;

AND NOT keeps all other bits and forces the selected bit to 0.

Toggle A Bit

woCommandWord := woCommandWord XOR 16#0001;

XOR flips the selected bit. Use this carefully in PLC logic, because cyclic execution can toggle the bit every scan if the trigger is not edge-detected.

Combine Masks

Several bits can be checked together.

_woCriticalFaultMask := 16#0008 OR 16#0010 OR 16#0020;
xboAnyCriticalFault := (woStatusWord AND _woCriticalFaultMask) <> 0;

This is useful for grouped diagnostics, alarm classes, and telegram status interpretation.

Bit Masking Checklist

  • Define masks with names when the meaning is not obvious.
  • Comment which external manual or telegram definition owns the bit layout.
  • Use edge detection before toggling bits from commands.
  • Avoid scattering the same mask literal across many blocks.
  • Convert bit fields into named Booleans near the interface boundary.