What Is BSWAP and How Does Endianness Swap Work?

This article provides an overview of endianness swap instructions, such as x86’s BSWAP, explaining their purpose and mechanics. You will learn the difference between Big-Endian and Little-Endian formats, why CPUs require dedicated instructions for byte reversal, and how hardware reorders byte sequences of binary words without altering individual bit patterns.


Understanding Endianness

Endianness refers to the order in which bytes of a multi-byte word are arranged in computer memory:

When a Little-Endian processor communicates over a network or parses specific file formats, it must convert data to or from Big-Endian.


What Is the BSWAP Instruction?

BSWAP (Byte Swap) is a hardware-level machine instruction introduced in the Intel 486 processor. Similar instructions exist in other architectures, such as REV in ARM.

Instead of requiring software to perform multiple shifts, masks, and logical OR operations to rearrange bytes, BSWAP reverses the byte order of a 32-bit or 64-bit general-purpose register in a single clock cycle.


How Binary Byte Inversion Works

A 32-bit word consists of 4 bytes (32 bits). Consider a 32-bit value divided into four 8-bit segments:

\[\text{Word} = [B_3][B_2][B_1][B_0]\]

Where: * \(B_3\) is bits 31–24 (Most Significant Byte) * \(B_2\) is bits 23–16 * \(B_1\) is bits 15–8 * \(B_0\) is bits 7–0 (Least Significant Byte)

When the BSWAP instruction executes on this register, it inverts the byte positions:

\[\text{BSWAP}([B_3][B_2][B_1][B_0]) \rightarrow [B_0][B_1][B_2][B_3]\]

Concrete Binary Example

Consider the 32-bit hexadecimal value 0x12345678:

Byte Index Hex Value Binary Representation
\(B_3\) 0x12 00010010
\(B_2\) 0x34 00110100
\(B_1\) 0x56 01010110
\(B_0\) 0x78 01111000

After running BSWAP, the new register value is 0x78563412:

New Position Byte Source Hex Value Binary Representation
Bits 31–24 \(B_0\) 0x78 01111000
Bits 23–16 \(B_1\) 0x56 01010110
Bits 15–8 \(B_2\) 0x34 00110100
Bits 7–0 \(B_3\) 0x12 00010010

Crucial Distinction: Bytes vs. Bits

An endianness swap only changes the sequence of the bytes within the word. It does not invert the individual bits within each byte. As shown in the table above, byte 0x12 remains 00010010 in binary; only its position within the 32-bit container changes from the highest 8 bits to the lowest 8 bits.


64-Bit Operations

On 64-bit systems, BSWAP handles 8-byte words (\(B_7\) through \(B_0\)):

\[\text{BSWAP}_{64}([B_7][B_6][B_5][B_4][B_3][B_2][B_1][B_0]) \rightarrow [B_0][B_1][B_2][B_3][B_4][B_5][B_6][B_7]\]

For 16-bit values, architectures typically use dedicated instructions (such as ROL r16, 8 or REV16 on ARM), as executing BSWAP on a 32-bit register holding a 16-bit value would place the swapped data into the upper 16 bits.


Hardware Implementation

In the CPU’s Arithmetic Logic Unit (ALU), byte-swapping does not require arithmetic computation. It is implemented using hardwired multiplexers and routing paths. The internal data bus simply connects the physical lines of the lower byte inputs directly to the higher byte outputs, allowing the transformation to complete instantly with minimal power and processing overhead.