Byte Swap

  1. This instruction is not supported on processors earlier than the 80486.




The BSWAP instruction reverses the byte order of the operand. This instruction can be used to change the endianness of the operand from little to big (or vice versa).

This instruction is only valid on 32 bit and 64 bit operands. To operate on a 16 bit operand, use the XCHG instruction. Use of this instruction on a 16 bit operand is undefined.


public void BSWAP(ref U32 arg)
    U32 temp = arg;
    arg.Bit[0..7] = temp.Bit[24..31];
    arg.Bit[8..15] = temp.Bit[16..23];
    arg.Bit[16..23] = temp.Bit[8..15];
    arg.Bit[24..31] = temp.Bit[0..7];

public void BSWAP(ref U64 arg)
    U64 temp = arg;
    arg.Bit[0..7] = temp.Bit[56..63];
    arg.Bit[8..15] = temp.Bit[48..55];
    arg.Bit[16..23] = temp.Bit[40..47];
    arg.Bit[24..31] = temp.Bit[32..39];
    arg.Bit[32..39] = temp.Bit[24..31];
    arg.Bit[40..47] = temp.Bit[16..23];
    arg.Bit[48..55] = temp.Bit[8..15];
    arg.Bit[56..63] = temp.Bit[0..7];

Flags Affected



Real-Address Mode

  • If the LOCK prefix is used.

Virtual-8086 Mode

  • If the LOCK prefix is used.

Protected Mode

  • If the LOCK prefix is used.

Compatibility Mode

  • If the LOCK prefix is used.

Long Mode

  • If the LOCK prefix is used.