Learn PLCs free
Data Movement & Bit ShiftingIntermediateIEC 61131-3

PLC Byte Swap: SWAP, SWPB & Endianness

A byte-swap instruction rearranges bytes inside a WORD or DINT without changing the individual byte values. It is used when a protocol or device sends a different byte order than the PLC expects. Define the source byte sequence and desired destination sequence before selecting a swap mode.

The SWAP function reverses the byte order of a word or double-word value. For a 16-bit WORD, SWAP exchanges the high byte and low byte. For a 32-bit DWORD, it reverses all four bytes. SWAP is essential when communicating between devices that use different byte orders (big-endian vs little-endian), such as Modbus communication with third-party devices, and when interpreting data from sensors that transmit bytes in a specific order.

Parameters

Inputs

NameTypeDescription
INANY_BITInput value with bytes to swap

Outputs

NameTypeDescription
OUTANY_BITOutput with reversed byte order

How byte-swap instruction works scan by scan

Read the waveforms and state table together. The diagram shows the sequence; the table states what the program should observe at each execution.

Byte-order mapping for WORD and DINT values

A WORD maps AB to BA. For a four-byte DINT, Rockwell SWPB modes can map ABCD to DCBA, CDAB or BADC. The correct map comes from a documented wire-format test vector.

Byte-order mapping for WORD and DINT valuesA WORD maps AB to BA. For a four-byte DINT, Rockwell SWPB modes can map ABCD to DCBA, CDAB or BADC. The correct map comes from a documented wire-format test vector.SOURCEMAPDESTAB→BA; ABCD→selected order
Blue is the source word, amber is the documented byte mapping, and green is the destination. The operation changes byte order only; it does not discover the protocol format.

State table

MomentInputInternal stateOutputEngineering meaning
Two-byte WORD16#12_34Swap high and low byte16#34_12The bit pattern is reordered; it is not numerically scaled.
DINT full reverseBytes A B C DREVERSED C B AUse when all four bytes arrive in reverse order.
DINT word swapBytes A B C DWORDC D A BSwaps the two 16-bit words while preserving byte order inside each word.
DINT byte swap per wordBytes A B C DHIGH/LOWB A D CSwaps bytes inside each 16-bit word without reversing word order.

Ladder and Structured Text examples

Swap a received register only after validating message quality

|----[ MessageDone ]----[/ MessageError ]----[ SWPB ]----|
|                                              Source: RawDint   |
|                                              Mode: WORD       |
|                                              Dest: OrderedDint |

Keep RawDint unchanged for diagnostics. Apply the documented mapping to a separate destination only after message quality and length are valid.

Use a known test pattern before decoding process data

IF MessageDone AND NOT MessageError THEN
    SWPB(RawDint, WORD, OrderedDint);
    MappingValid := (RawDint = 16#11223344)
                AND (OrderedDint = 16#33441122);
END_IF;

The syntax and mode names shown are Rockwell-specific. On another platform, implement the equivalent documented mapping and test the same hexadecimal vector.

Vendor compatibility

A shared mnemonic does not guarantee an identical interface or state machine. Confirm the instruction help for the controller, firmware, language and library version used on the project.

PlatformInstructionWhat maps cleanlyCaveat to verify
Rockwell LogixSWPBINT AB→BA; DINT supports REVERSE, WORD and HIGH/LOW order modesINT-to-DINT destinations are sign extended after the byte swap; shared or overwritten destinations can create unexpected results.
Siemens S7SWAP / SWAP instruction familyReverses byte order for two-byte and four-byte data elements on supported CPUsCPU generation and data type determine available operations. Confirm whether the operation is byte, word or complete-order reversal.
Protocol/device mappingDriver byte/word order optionSome drivers can normalize the value before PLC logic receives itDo not swap in both the driver and PLC. Record exactly where normalization occurs.

Failure modes and edge cases to test

FLOAT appears nonsensical after a swap

Verify both 16-bit register order and byte order inside each register before reinterpreting the IEEE-754 bit pattern.

Negative INT becomes a large DINT

Check signedness and Rockwell sign-extension behavior when an INT source writes a DINT destination.

Only some device models need the swap

Bind mapping to a device/profile version and fail configuration rather than guessing at runtime.

Value is correct in the driver but wrong in PLC logic

The driver may already have normalized byte order. Remove duplicate swapping and preserve the raw-wire evidence.

Odd register or byte count

Reject the message or handle the trailing data explicitly; do not read beyond a validated buffer.

Verification checklist

  1. 1Obtain the device register map and state byte and word order explicitly.
  2. 2Capture one known hexadecimal wire-format test vector.
  3. 3Test WORD and DINT mappings without interpreting the value as REAL first.
  4. 4Verify signed/unsigned destination behavior and array bounds.
  5. 5Document whether the device, driver, gateway or PLC owns normalization.

Current primary and technical sources

These sources support the behavior summarized here. The exact controller, firmware, instruction and installed-library help remain authoritative for implementation.

Platform-Specific Implementation

S

Siemens (TIA Portal)

Use SWAP instruction in TIA Portal for WORD/DWORD byte swapping.

AB

Allen-Bradley (Studio 5000)

Use SWPB (Swap Byte) instruction in Studio 5000.

C

CODESYS

Implement using SHL, SHR, AND, OR operations or vendor library functions.

Common Applications

  • Modbus byte order correction
  • Endianness conversion
  • Cross-platform data exchange
  • Sensor data interpretation
  • Protocol byte reordering

Frequently Asked Questions

What is SWAP (Byte Swap) in PLC programming?

Reverses byte order of a word value for endianness conversion. The SWAP function reverses the byte order of a word or double-word value. For a 16-bit WORD, SWAP exchanges the high byte and low byte.

What are common applications of SWAP (Byte Swap)?

SWAP (Byte Swap) is commonly used for: Modbus byte order correction, Endianness conversion, Cross-platform data exchange, Sensor data interpretation, Protocol byte reordering.

How do I use SWAP (Byte Swap) in different PLC platforms?

Siemens: Use SWAP instruction in TIA Portal for WORD/DWORD byte swapping. Allen-Bradley: Use SWPB (Swap Byte) instruction in Studio 5000. CODESYS: Implement using SHL, SHR, AND, OR operations or vendor library functions.

Free PLC simulator

Wire this block up and run it

Drop the instruction into a rung, hit Run, and watch it execute in your browser. 12 guided lessons across 8 PLC dialects — free account, no credit card.

Practice PLCs free →