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
| Name | Type | Description |
|---|---|---|
| IN | ANY_BIT | Input value with bytes to swap |
Outputs
| Name | Type | Description |
|---|---|---|
| OUT | ANY_BIT | Output 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.
State table
| Moment | Input | Internal state | Output | Engineering meaning |
|---|---|---|---|---|
| Two-byte WORD | 16#12_34 | Swap high and low byte | 16#34_12 | The bit pattern is reordered; it is not numerically scaled. |
| DINT full reverse | Bytes A B C D | REVERSE | D C B A | Use when all four bytes arrive in reverse order. |
| DINT word swap | Bytes A B C D | WORD | C D A B | Swaps the two 16-bit words while preserving byte order inside each word. |
| DINT byte swap per word | Bytes A B C D | HIGH/LOW | B A D C | Swaps 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.
| Platform | Instruction | What maps cleanly | Caveat to verify |
|---|---|---|---|
| Rockwell Logix | SWPB | INT AB→BA; DINT supports REVERSE, WORD and HIGH/LOW order modes | INT-to-DINT destinations are sign extended after the byte swap; shared or overwritten destinations can create unexpected results. |
| Siemens S7 | SWAP / SWAP instruction family | Reverses byte order for two-byte and four-byte data elements on supported CPUs | CPU generation and data type determine available operations. Confirm whether the operation is byte, word or complete-order reversal. |
| Protocol/device mapping | Driver byte/word order option | Some drivers can normalize the value before PLC logic receives it | Do 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
- 1Obtain the device register map and state byte and word order explicitly.
- 2Capture one known hexadecimal wire-format test vector.
- 3Test WORD and DINT mappings without interpreting the value as REAL first.
- 4Verify signed/unsigned destination behavior and array bounds.
- 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.
- Swap Byte (SWPB), Logix Designer V38
Rockwell Automation — Supported types, DINT order modes, sign extension and execution
- S7-1200 System Manual
Siemens — SWAP instruction behavior for two-byte and four-byte elements
Platform-Specific Implementation
Siemens (TIA Portal)
Use SWAP instruction in TIA Portal for WORD/DWORD byte swapping.
Allen-Bradley (Studio 5000)
Use SWPB (Swap Byte) instruction in Studio 5000.
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.
Related Function Blocks
MOVE (Data Move)
Copies a value from source to destination for any data type.
View Reference →SHL (Shift Left)
Shifts bits left by N positions, filling right with zeros. Equivalent to multiply by 2^N.
View Reference →SHR (Shift Right)
Shifts bits right by N positions. Equivalent to integer division by 2^N.
View Reference →ROL (Rotate Left)
Rotates bits left; bits shifted out the left wrap around to the right.
View Reference →