Learn PLCs free
Data Movement & Bit ShiftingIntermediateIEC 61131-3

SWAP (Byte Swap)

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. 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

Ladder Logic Example

// Ladder Logic - SWAP (Modbus Byte Order)
//
// |----[SWAP]----|
// | IN: Modbus_Raw_Word    |
// | OUT: Corrected_Word    |

Structured Text Example

// Swap bytes of a WORD value
Corrected_Word := SWAP(Modbus_Raw_Word);
// Example: 16#1234 becomes 16#3412

20 Production-Ready Ladder Logic Templates

Stop writing the same rungs from scratch. Get copy-paste-ready templates for motor control, PID loops, safety interlocks, conveyors, and more.

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.

Stop Writing Ladder Logic From Scratch

Get 20 production-ready ladder logic templates for $29 -- download instantly and use them on your next project today.

20
Ready-Made Templates
LD + ST
Both Languages Included
$29
One-Time Purchase
Get Instant Access -- $29

Instant download - 30-day money-back guarantee - Use on unlimited projects

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 →