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
| Name | Type | Description |
|---|---|---|
| IN | ANY_BIT | Input value with bytes to swap |
Outputs
| Name | Type | Description |
|---|---|---|
| OUT | ANY_BIT | Output 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
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 →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.
Instant download - 30-day money-back guarantee - Use on unlimited projects