Skip to main content

SETBIT

Sets a specific bit in a data field.

Ladder Representation

SETBIT Ladder block representation

Block Structure

Variable TypeNameData TypeDescription
VAR_IN_OUTDataBYTE USINT SINT WORD UINT INT DWORD UDINT DINTVariable whose bit is changed.
VAR_INPUTENBOOLBlock enable.
VAR_INPUTPositionBYTE USINTPosition of the bit to be changed.
VAR_OUTPUTDoneBOOLOperation success.

Operation

When EN is TRUE, the block sets the bit indicated by Position in Data and returns Data with the updated value.

When EN is FALSE, Data remains unchanged.

DONE receives the EN value, except when a bit set error occurs. In that case, DONE receives FALSE.

Position Range

Make sure Position is within the valid bit range for the Data type. For example, BYTE has 8 bits, so Position must be between 0 and 7.

Execution Flowchart

SETBIT execution flowchart

Examples

SETBIT Ladder example 1

Example 1 sets bit position 0 of AUX, whose initial value is 153, or 1001 1001 in binary. Because this bit is already TRUE, the value is not changed.

SETBIT Ladder example 2

Example 2 sets bit position 2 of AUX, changing its binary value and decimal representation.

SETBIT Ladder example 3

Example 3 sets bit position 15 of AUX. Because AUX is a BYTE variable with only 8 bits, the block generates an execution error and the output is not enabled.