Skip to content

XOR — Bitwise Exclusive OR

Category
Move / Logical
Type
Output
Availability
All platforms

XOR instruction block: Source A Inputs_Now, Source B Inputs_Prev, Dest Changed


XOR (Bitwise Exclusive OR) writes to Dest a word whose every bit is 1 where Source A and Source B differ, and 0 where they agree. It is an output instruction with no state and no status bits. Use XOR to find which bits changed between two readings of a word, to toggle chosen bits with a mask, or to compare two words for any difference. Do not use XOR for a single bit toggle that must happen once per event; without an ONS ahead of it the bit flips every scan.


Operand Type Format Valid Range Required Description
Source A A whole-number tag, member, element or bit, or a literal Tag, Tag.Member, Tag[n], Tag.bit, a number Any resolvable reference Yes The first word.
Source B Same as Source A Same Any resolvable reference Yes The second word, or the toggle mask.
Dest A writable whole-number tag, member or element Tag, Tag.Member, Tag[n] SINT, INT or DINT; not a literal, not an input pin Yes Where the result is written.

A REAL operand is truncated to a whole number first. Dest may be one of the sources.


Nothing. Dest keeps its power-up value.

The instruction does nothing. Dest holds its last value.

Both sources are read, XORed bit by bit, and the result is written to Dest every scan the rung is true.

Nothing.


Scenario: A block of digital inputs is read into one word each scan, and the previous scan’s word is kept. XOR of the two shows which inputs changed since last scan, so a single NEQ Changed 0 can trigger a log entry.

Tags:

  • Compare_Now — Compare the two words, BOOL
  • Inputs_Now — Inputs this scan, INT
  • Inputs_Prev — Inputs last scan, INT
  • Changed — Bits that differ, INT

Rung 1:

—] [Compare_Now———[XOR Inputs_Now Inputs_Prev Changed]———

Scan 1 — Compare_Now = 0. Inputs_Now reads 45 and Inputs_Prev reads 41. The rung is false: Changed holds 0.

Figure 1 — XOR rung false: Inputs_Now 45, Inputs_Prev 41, Changed 0, rung dark

Scan 2 — Compare_Now = 1. Changed reads 4: only bit 2 differs.

Figure 2 — XOR rung true: Changed reads 4, rung lit


XOR — Inputs_Now XOR Inputs_Prev, INT (16 bits)
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Inputs_Now 45 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 1
Inputs_Prev 41 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1
Changed 4 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
^
1 where they differ

Bits 5, 3 and 0 are set in both words and come out 0. Bit 2 is set in one and clear in the other and comes out 1.



Toggle bits. XOR Word 0x0001 Word flips bit 0 each time it runs. Put an ONS ahead of it or the bit flips on every scan.

Two words equal. XOR gives 0 exactly when the two words are identical, which is what EQU A B tests without a Dest.

Round out the change detector. Follow the XOR with MOV Inputs_Now Inputs_Prev on the same rung so the previous word is refreshed after the comparison.

The block never colours. Watch the rung wire and the Dest value line.

Applies to LadderIDE >=1.2.2 · Last reviewed 2026-09-11 · Screenshots verified 2026-09-11