Bitwise (output instruction)

AND: Bitwise AND

Writes the bitwise AND of two registers.

Also called: word AND, mask off bits.

AND(number, a, b)

Arguments

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

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

This is not the AND you get from two contacts in series. That one is about a single rung and a single bit. This one takes two whole registers and works down them bit by bit, writing a 1 wherever both inputs had a 1 and a 0 everywhere else. The confusion between the two is the single most common misunderstanding on this page, and it is worth being deliberate about: series contacts are boolean logic, AND is word logic.

Its everyday job is masking. AND a status word with 255 and you have kept the low byte and thrown away everything above it. AND with 1 and you have isolated bit 0. The second operand is a pattern of the bits you want to keep.

Because anything ANDed with 0 is 0, the mask is a filter: a bit that is 0 in the mask can never survive into the result, whatever the source held.

A rung you can run

Keep the low byte of a packed word

RUNG 1PRead_StatusMOV Packed_Word:= 52651RUNG 2AND Low_BytePacked_W… and 255RUNG 3Low_Byte≥ 128Top_Bit_Of_Byte
ONS(Read_Status)MOV(Packed_Word,52651)
AND(Low_Byte,Packed_Word,255)
GEQ(Low_Byte,128)OTE(Top_Bit_Of_Byte)

Press Read_Status to load a word with rubbish in the top half. The AND with 255 keeps only the lowest eight bits, giving 171, and everything above is gone. Change the mask to 15 and you keep only the lowest four bits, giving 11.

Run this rung in the simulator

What it is called on other controllers

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

Common mistakes

  • Confusing it with contacts in series. If what you want is two conditions both being true on one rung, put two contacts next to each other. AND is for when you have two registers.
  • Writing the mask as a decimal number that looks tidy. A mask of 100 is not the first two digits of anything; in bits it is 1100100, an odd scatter of positions. Work masks out from bit values, or from hexadecimal.

Practise it

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

Related instructions

All ladder logic instructions