Bitwise (output instruction)

XOR: Bitwise Exclusive OR

Writes the bitwise exclusive OR of two registers.

Also called: word XOR, exclusive or, change detect.

XOR(number, a, b)

Arguments

NameWhat it isAcceptsDefault
destinationWhat the instruction acts ona registerrequired
aValue Aa number or a tag0
bValue Ba number or a tag0

Arguments are positional and this table is generated from the simulator's own instruction registry, so it always matches what the engine accepts.

What it does

XOR writes a 1 wherever the two inputs differ and a 0 wherever they agree. That description is also its best use: XOR is how a program notices change.

Keep a copy of an input word from the previous scan, XOR the current word against the copy, and the result has a 1 in every bit position that just moved. One instruction gives you edge detection across all 32 bits at once, where doing it with one-shots would take 32 rungs.

The order of the three steps is the whole trick, and it is easy to get wrong: compare first, act on the result, and only then save the current value as the new copy. Saving first makes the two words identical and the answer is always zero.

XOR also flips bits: a bit XORed with 1 comes out inverted, and XORed with 0 is left alone, so it is a toggle where AND is a filter and OR is a setter.

A rung you can run

Which inputs changed since the last scan

RUNG 1XOR Input_ChangedInput_Wo… xor Input_Wo…RUNG 2Input_Changed≠ 0Something_MovedRUNG 3AND Watched_ChangedInput_Ch… and 12RUNG 4Watched_Changed≠ 0Watched_Input_MovedRUNG 5MOV Input_Word_Last:= Input_Wo…
XOR(Input_Changed,Input_Word,Input_Word_Last)
NEQ(Input_Changed,0)OTE(Something_Moved)
AND(Watched_Changed,Input_Changed,12)
NEQ(Watched_Changed,0)OTE(Watched_Input_Moved)
MOV(Input_Word_Last,Input_Word)

Change Input_Word from the tag panel and the first output pulses for exactly one scan, because the last rung then copies the new value and the two words agree again. The AND in the middle narrows it to bits 2 and 3, so only those inputs raise the second output. Move the MOV to the top of the program and nothing ever fires again.

Run this rung in the simulator

What it is called on other controllers

PlatformEquivalent
Rockwell (Studio 5000)XOR
Siemens (TIA Portal)XOR
Mitsubishi (GX Works)WXOR
CODESYS and IEC 61131-3XOR

Common mistakes

  • Storing the copy before doing the comparison. The two words then always match, the result is always zero, and the change detection silently never fires.
  • Forgetting the copy has to be updated every scan the logic runs. If the save rung is conditional and the compare is not, the result reports every change since the last time the condition happened to be true.

Practise it

Interactive exercises that use XOR. Each one checks your ladder against a real scan.

Related instructions

All ladder logic instructions