Compare (input instruction)

MEQ: Masked Equal

Compares two values through a mask, so only the bits you care about are tested.

Also called: masked compare, masked equality test.

MEQ(source, mask, compare)

What it does

MEQ answers a question a plain compare cannot: are these particular bits the way I expect, never mind the rest. It takes a source, a mask and a value to compare against, and it passes power when the source and the compare value agree on every bit the mask selects. Bits the mask does not select are ignored entirely.

The mask is a number read as bits. Bit 0 is worth 1, bit 1 is worth 2, bit 2 is worth 4, bit 3 is worth 8, and a mask of 3 therefore means the lowest two bits and nothing else. Setting a mask is really writing down which bit positions matter and adding up their values.

This is what you reach for with a status word. A drive, a scanner or a networked device hands you one register with a dozen unrelated bits in it, most of which change constantly, and you need to act on two of them. MEQ tests those two and stays blind to the rest, where a plain EQU on the whole word would go false the moment any other bit moved.

A rung you can run

Two bits out of a drive status word

RUNG 1PSim_StatusMOV Drive_Status:= 11RUNG 2MEQDrive_Status&3 =3Drive_ReadyRUNG 3MEQDrive_Status&12 =8Drive_At_Speed
ONS(Sim_Status)MOV(Drive_Status,11)
MEQ(Drive_Status,3,3)OTE(Drive_Ready)
MEQ(Drive_Status,12,8)OTE(Drive_At_Speed)

The first rung loads 11 into the status word so there is something to look at: 11 is 1011, so bits 0, 1 and 3 are set. The second rung masks with 3, testing only bits 0 and 1, finds both set, and Drive_Ready comes on. The third masks with 12, testing bits 2 and 3, and finds bit 3 set and bit 2 clear, which is exactly the pattern 8 it was given. Change the value in the MOV to watch each test hold or fail independently.

Run this rung in the simulator

What it is called on other controllers

PlatformEquivalent
Rockwell (Studio 5000)MEQ
Siemens (TIA Portal)No single masked compare: AND the value with the mask, then compare
Mitsubishi (GX Works)No single masked compare: WAND, then a comparison contact
CODESYS and IEC 61131-3No single masked compare: (value AND mask) = pattern

Common mistakes

  • Writing the mask in decimal digits rather than bit values. A mask of 11 does not mean the first two bits, it means bits 0, 1 and 3, because 11 is 1011 in binary. Work the mask out by adding the values of the bit positions you want.
  • Leaving bits set in the compare value that the mask does not select. It does no harm, because both sides are masked before the comparison, but it makes the rung read as though those bits are being tested, and the next person will believe it.

Practise it

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

Related instructions

All ladder logic instructions