Skip to content

10Expressions and operators

Boolean logic, comparisons and bit operations

AND, OR, XOR and NOT with their truth tables, the six comparison operators, then bit shifts and AND-masking of status words.

Lesson 10 of 3411 minIEC 61131-3

The four boolean operators

In ST the logic is words, not symbols: AND, OR, XOR, NOT.

A

B

A AND B

A OR B

A XOR B

NOT A

0

0

0

0

0

1

0

1

0

1

1

1

1

0

0

1

1

0

1

1

1

1

0

0

AND is contacts in series, OR is contacts in parallel. XOR has no direct ladder equivalent, which is exactly why it earns its keep: it is TRUE only when the two signals disagree.

XOR on the job

Its most common industrial use is discrepancy detection:

SensorFault := LSH XOR (Level >= 90.0);

A high level switch and an analogue level transmitter should say the same thing. If they disagree, one of them is broken. The same line compares a valve open feedback against the open command.

Comparison operators

Operator

Meaning

=

equal

<>

not equal

< >

less than, greater than

<= >=

less or equal, greater or equal

A comparison produces a BOOL, so you can assign it or pass it straight on:

HiAlm := Level >= 90.0;
Ready := (State = 0) AND (Faults = 0);

Never compare reals with =. Level = 50.0 may never be true once, because of floating point representation. Use a band:

AtSetpoint := ABS(Level - 50.0) < 0.5;

Bits inside a word

A status word from a VFD or a comms module packs several flags into one integer. To read one flag you use a mask: AND the word with a number that has a single bit set.

(* bit 0 ready, bit 1 running, bit 3 faulted *)
IsReady   := (StatusWord AND 1) <> 0;
IsRunning := (StatusWord AND 2) <> 0;
IsFaulted := (StatusWord AND 8) <> 0;

To set a bit: Ctrl := Ctrl OR 4;. To clear it: Ctrl := Ctrl AND 65531;. To toggle it: Ctrl := Ctrl XOR 4;.

Shift and rotate functions

The standard defines four word functions:

Function

What it does

SHL(IN, N)

shift left, zeros come in

SHR(IN, N)

shift right, zeros come in

ROL(IN, N)

rotate left, what leaves the left re-enters on the right

ROR(IN, N)

rotate right

A shift left by one doubles the value, a shift right halves it, and both are cheaper than multiply and divide on a small CPU.

(* vendor code: this does NOT run in the lab on this site *)
Doubled  := SHL(Value, 1);
Halved   := SHR(Value, 1);
Walking  := ROL(Pattern, 1);     (* a running light across eight outputs *)
HighByte := SHR(Word16, 8) AND 255;

Note: the lab on this site supports AND, OR, XOR, NOT and MOD only. It does not support SHL, SHR, ROL or ROR. The block above is there to read and to export to your vendor's environment; the lab program in this lesson sticks to the supported operators.

Multiplying and dividing by 2 or 256 gives the same answer as a shift and does run in the lab:

Doubled  := Value * 2;
HighByte := (Word16 / 256) MOD 256;

Try it in the lab

The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.

What to try

  1. Run the program and turn on S1 only: L1 stays dark because one vote is not enough, but L2 lights because S1 and S2 disagree.
  2. Turn S2 on too: L1 and L4 light.
  3. Turn S4 on (the bypass): L4 goes dark while L1 stays lit.
  4. In the ladder notice how each OR became a parallel branch.

People find this lesson by searching

AND OR XOR plctruth table boolean logicstructured text comparison operatorsXOR structured textSHL SHR ROL ROR plcbit mask status word plccompare real values plc

Course outline

  1. 01What is Structured Text, and when to use it instead of ladder
  2. 02How a PLC runs your code: the scan cycle and the process image
  3. 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
  4. 04Your first program: starting a pump with seal-in logic
  5. 05Data types: BOOL, INT, REAL, TIME and the rest
  6. 06Declaring variables: VAR, VAR_INPUT and RETAIN
  7. 07Literals and constants: 16#FF, 2#1010 and T#5s
  8. 08Type conversion and scaling a 4-20 mA signal
  9. 09Assignment and arithmetic in ST: := and operator precedence
  10. 10Boolean logic, comparisons and bit operations
  11. 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
  12. 12IF, ELSIF and ELSE: the rules and when to avoid them
  13. 13The CASE statement and your first state machine
  14. 14The FOR loop and the watchdog
  15. 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
  16. 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
  17. 17Timers: TON, TOF and TP
  18. 18Counters: CTU, CTD and CTUD
  19. 19SR and RS flip-flops: which one wins
  20. 20Writing your own function
  21. 21Writing a function block
  22. 22Arrays in Structured Text
  23. 23Structs and enumerations
  24. 24References and pointers
  25. 25The state machine pattern: a batch sequence that cannot get stuck
  26. 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
  27. 27Analogue signal processing: counts to engineering units, NE43 and filtering
  28. 28Closed-loop control in ST: hysteresis first, then a practical PID
  29. 29Motors and valves in practice: permissives, feedback and travel time
  30. 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
  31. 31Testing and debugging ST: the watch table, forcing and simulation
  32. 32An ST style guide: naming, comments, constants and version control
  33. 33Final project: a complete tank station with a state machine and alarms
  34. 3420 interview questions on ST and PLC programming, with answers