SETBIT
Sets a specific bit in a data field.
Ladder Representation
Block Structure
| Variable Type | Name | Data Type | Description |
|---|---|---|---|
| VAR_IN_OUT | Data | BYTE USINT SINT WORD UINT INT DWORD UDINT DINT | Variable whose bit is changed. |
| VAR_INPUT | EN | BOOL | Block enable. |
| VAR_INPUT | Position | BYTE USINT | Position of the bit to be changed. |
| VAR_OUTPUT | Done | BOOL | Operation 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.
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

Examples
- Ladder Example
- Structured Text Example
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.
Example 2 sets bit position 2 of AUX, changing its binary value and decimal representation.
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.
The following Structured Text code applies the same logic shown in the Ladder example.
VAR
AUX : BYTE := 2#1001_1001;
POS : BYTE := 2;
END_VAR
DO1 := FB_SETBIT(
EN:=DI1,
Data:=AUX,
Position:=POS);