You can address individual bits in integer variables. To do this, append the variable with a dot and the index of the addressed bit. The bit-index can be given by any constant. Indexing is 0-based.
Syntax
<integer variable name> . <index> <integer data typ> = BYTE | WORD | DWORD | LWORD | SINT | USINT | INT | UINT | DINT | UDINT | LINT | ULINT
Example
In the program, the third bit of the variable wA
is set to the value of variable xB
. The constant c_usiENABLE
acts as an index to access the third bit of the variable iX
.
Index access
PROGRAM PLC_PRG VAR wA : WORD := 16#FFFF; xB : BOOL := 0; END_VAR // Index access in an integer variable wA.2 := xB;
Result: wA = 2#1111_1111_1111_1011 = 16#FFFB
Constant as index
// GVL declaration VAR_GLOBAL CONSTANT gc_usiENABLE : USINT := 2; END_VAR PROGRAM PLC_PRG VAR iX : INT := 0; END_VAR // Constant as index iX.gc_usiENABLE := TRUE; // Third bit in iX is set TRUE
Result: iX = 4