Swap Byte (SWPB)

This information applies to the
CompactLogix
5370,
ControlLogix
5570,
Compact GuardLogix
5370,
GuardLogix
5570,
Compact GuardLogix
5380,
CompactLogix
5380,
CompactLogix
5480,
ControlLogix
5580, and
GuardLogix
5580 controllers.
The SWPB instruction rearranges the order of the bytes of the Source. It places the result in the Destination.
Available Languages
Ladder Diagram
SWPB_LD_avail_v31
Function Block
This instruction is not available in function block.
Structured Text
SWPB(Source, Order Mode, Dest);
Operands
IMPORTANT:
Unexpected operation may occur if:
  • Output tag operands are overwritten.
  • Members of a structure operand are overwritten.
  • Except when specified, structure operands are shared by multiple instructions.
There are data conversion rules for mixing numeric data types within an instruction. See
Data Conversion
.
Ladder Diagram and Structured Text
Operand
Data Type
Format
Description
Source
INT
DINT
tag
Tag that contains the bytes to rearrange.
Order Mode
list item
This operand specifies how to reorder. Refer Order Mode table.
Dest
INT
DINT
tag
Tag to store the bytes in a new order. Refer Dest table.
If selecting the HIGH/LOW order mode, enter it as HIGHLOW (without the slash). See Structured Text Syntax for more information on the syntax of expressions within structured text.
Order Mode
If the Source is an
And you want to change the bytes to this pattern(each letter represents a different byte)
Then select
INT
AB => BA
Any option
DINT
ABCD => DCBA
REVERSE
ABCD =>CDAB
WORD
ABCD => BADC
HIGH/LOW
Dest
If the Source is an
Then the Destination must be an
INT
INT, DINT
If the destination is a DINT, the result is sign extended after bytes swap.
DINT
DINT
Affects Math Status Flags
No
Major/Minor Faults
None specific to this instruction. See Index Through Arrays for array-indexing faults.
Execution
Ladder Diagram
Condition/State
Action Taken
Prescan
N/A
Rung-condition-in is false
N/A
Rung-condition-in is true
The instruction rearranges the specified bytes.
Postscan
N/A
Structured Text
Condition/State
Action Taken
Prescan
See Prescan in the Ladder Diagram table.
Normal Execution
See Rung-condition-in is true in the Ladder Diagram table.
Postscan
See Postscan in the Ladder Diagram table.
Examples
Example 1 - Swap the bytes of a DINT tag
The three SWPB instructions reorder the bytes of DINT_1 according to a different order mode. The display style is ASCII, and each character represents one byte. Every instruction places the bytes, in the new order, in a different Destination.
Ladder Diagram
SWPB_LD_ex1_v31
Example 2 - Swap the bytes in all elements of an array
Ladder Diagram
SWPB_LD_ex2_#1_v31
SWPB_LD_ex2_#2_[index]_v31
SWPB_LD_ex2_#3_v31
Example 3: SWPB on Structured Text
Structured Text
index := 0;
SIZE (array[0],0,array_length);
REPEAT
SWPB(array[index],REVERSE,array_bytes_reverse[index]);
index := index + 1;
UNTIL(index >= array_length)END_REPEAT;
Provide Feedback
Have questions or feedback about this documentation? Please submit your feedback here.