Skip to main content

Bit String

Bit Shift

note

All bit shift functions can return either their input type or the bitstring type with the same number of bits. For example, if the input is a DINT, the return can be assigned directly to either a DINT or a DWORD without conversion. The number of bits to shift or rotate is a USINT type.

SHL - Shift Left

Shifts all bits of the input left by N places.

VAR
a : UINT := 51919;
END_VAR

printf(a, 'b16'); // 1100 1010 1100 1111
a := SHL(a, 4);
printf(a, 'b16'); // 1010 1100 1111 0000

SHR - Shift Right

Shifts all bits of the input right by N places.

VAR
a : UINT := 51919;
END_VAR

printf(a, 'b16'); // 1100 1010 1100 1111
a := SHR(a, 4);
printf(a, 'b16'); // 0000 1100 1010 1100

ROL - Rotate Left

Rotates all bits of the input left by N places. This preserves all bits of the input.

VAR
a : UINT := 51919;
END_VAR

printf(a, 'b16'); // 1100 1010 1100 1111
a := ROL(a, 4);
printf(a, 'b16'); // 1010 1100 1111 1100

ROR - Rotate Right

Rotates all bits of the input right by N places. This preserves all bits of the input.

VAR
a : UINT := 51919;
END_VAR

printf(a, 'b16'); // 1100 1010 1100 1111
a := ROR(a, 4);
printf(a, 'b16'); // 1111 1100 1010 1100