Search

A technology, a device or an article

CPU byte order: what BSWAP actually reverses

BSWAP reverses the bytes of a register. How that differs from format conversion, bit reversal, and structure serialization.

Four colored cards change order while retaining their individual patterns.
ITNWS AI-generated conceptual illustration. Whole bytes change places, not individual bits; the cards do not depict a CPU die.ITNWS · image_gen · Original ITNWS AI-generated conceptual editorial illustration

Imagine a short network message field: four bytes 12 34 56 78, written here in hexadecimal in arrival order. The author of the format said that the most significant byte comes first. This means the field represents the number 0x12345678. Now the same bytes are in the memory of the x86 computer, and the program loads them as one 32-bit integer. The register holds 0x78563412. The message has not been corrupted. It’s just that the CPU’s memory reading rule and the format’s field writing rule turned out to be different. The story begins with an agreement that is all too easily mistaken for a property of the four pieces of data themselves.

Let’s pause near this line. The order 12 34 56 78 refers to increasing memory addresses. The notation 0x12345678 refers to a numerical value: on the left we usually show the most significant digits. These ways of arranging symbols do not have to match. In little-endian, the low byte of a multibyte value is placed at a smaller address. Therefore, a memory containing the number 0x12345678 in normal 32-bit x86 representation would show 78 56 34 12 when inspected byte by byte. The number remains the same; the viewpoint from which the observer read its representation changed.

The word “reverse” is tricky here. Reversed relative to which convention? A numerical value does not have its own left edge on the paper. The sequence of bytes has an order that is specified by the protocol, file, or architecture. If you look only at the dump, it is easy to declare one side to be reversed, although both sides follow their own rules. The real error occurs when a program crosses a convention boundary without converting. An unusual dump in itself does not indicate a processor error or memory corruption.

In Intel SDM Volume 2, the BSWAP instruction describes the exact swapping of register bytes. For a 32-bit operand 0x78563412 the result is 0x12345678. Every eight bits stay together: 0x12 does not become 0x48, as would happen if all the individual bits were reversed. Entire bytes are swapped. This difference is clearly visible in our four-byte example, where each element is recognizable. The card illustration preserves the design of each card for this reason; it is conceptual and does not depict the internal layout of the CPU.

When a byte swap performs format conversion

Let’s return to the message received. We loaded the big-endian field with a normal little-endian load, got the numeric value with the bytes swapped, then applied BSWAP. Now the arithmetic can work with the expected value. But the instruction did not know it had received a network field. It didn’t read the packet description, determine its length, or check to see if the four bytes were even available. It only has a register and a permutation rule. The meaning of the translation comes from how the program prepared the operand and what it plans to do with the result.

Try mentally applying BSWAP to a number that has already been correctly decoded. The result will be another value, entirely consistent with the instruction and completely unnecessary for the task. This is exactly how a double conversion occurs: the library has already read the field according to the format rules, and the next layer repeated the usual “fix endian” operation. The convenient label “normalized” does not help if the state of the input data is not named. Normalization is needed at a certain boundary and must have a single clear owner.

Hence the useful distinction between unconditional byte swap and host-to-big-endian conversion. The first one always rearranges bytes. The second expresses a relationship between two representations: on a little-endian machine, a permutation is required; on a big-endian machine, such a transformation may not change the bits at all. The endian(3) family on Linux names the direction directly: htobe32 and be32toh. The name tells more than the desire to “make a swap”: where the data came from and in what form it is needed next. This is a library interface, not a promise of one specific machine instruction.

The same story is familiar from htonl and ntohl: these integer functions define network byte order as big-endian. However, a protocol is not required to use this order in all of its fields just because it travels across the network. It can explicitly choose little-endian, variable-length, or its own encoding. A correctly named function is not a substitute for reading the message specification. It is especially unpleasant when adjacent fields differ in format and the developer tries to “reverse the entire packet” once.

Etched register compartments connect to the reverse order of tiles in memory.
ITNWS AI-generated conceptual illustration. A conceptual relationship between a register and byte order, not a literal hardware schematic. — ITNWS · image_gen · Original ITNWS AI-generated conceptual editorial illustration

If a hypothetical header contains a 16-bit length, a 32-bit identifier, and two single-byte flags, each multibyte field has its own boundary. Reversing all eight bytes of the header also means rearranging the fields themselves. This is a different structure, and not a translation of the byte order within the values. A one-byte flag does not require endian conversion at all: there are no multiple bytes within one byte that need to be swapped. But the meaning of individual flag bits is still determined by the format. Byte order and the numbering of bit flags remain different issues.

There is also a temptation to show the register in the debugger and say: “Here is a big-endian number.” For a regular integer register, this is an imprecise formulation. It contains the bits of a value; endian is especially significant when mapping these bits to memory bytes or an external sequence. Printing a register as a hexadecimal string usually goes with the most significant digits first, regardless of how the value is stored by addresses. You can simultaneously see the familiar number in the register window and the unusual order in the memory window without any contradiction.

BSWAP has 32-bit and 64-bit operands. Having a 64-bit processor does not mean that every use of this instruction reverses eight bytes. For the corresponding 64-bit encoding, REX.W is used. In 64-bit mode, the result of writing a 32-bit register also interacts with the normal rule of zeroing its top half. Therefore, reading the disassembly requires looking at the width of the operand. Trying to use the 16-bit form of BSWAP does not provide a valid, universal way to reverse a word: Intel marks the result as undefined.

This in itself does not make converting 16-bit fields difficult. It can be expressed by a library or a suitable sequence of instructions. It is more important here not to impose a particular machine instruction on the source code. The compiler has the right to choose an implementation taking into account the architecture, neighboring loads and optimization. A be16toh call expresses the intended conversion, while the BSWAP name reports a specific CPU operation with specified operand widths. These levels can cooperate well if they are not forced to be the same concept.

Representation does not automatically define a structure

In our original message, it was convenient to immediately load four bytes into an integer. In a real program, this requires compliance with language access rules and memory availability. A pointer to an arbitrary byte buffer cannot always be simply cast to a pointer to a 32-bit object and dereferenced. In addition to alignment, there are requirements for object types and lifetimes. Rearranging the bytes only corrects their order. It does not retroactively convert an illegal load into an acceptable load. Therefore, safely obtaining the value itself and converting it should be discussed separately.

To explain, we can assemble a number arithmetically from four unsigned bytes, converting each one in advance to a suitable sufficiently wide unsigned type. Then the byte weight is set explicitly, and the alignment of the source address does not become a hidden condition for multibyte reads. This is not a recommendation to always prefer manual assembly over a library. This is a way to see that format decoding determines the numerical value even before the question of the best instruction sequence. The transformation must comply with the rules of the selected language and interface, and not just the drawing in memory.

Copying an entire C structure to a file adds other differences. There may be padding between fields, the sizes of some types are implementation dependent, and the placement of bit fields has its own rules. Even if each multibyte member is separately converted to the required byte order, this does not yet prove that the entire structure corresponds to the external format. The protocol requires certain offsets and lengths. Endianness defines one important aspect of representation; serialization must define the rest. Therefore, a memory dump does not become a portable file through a single swap call.

Strings make the distinction even clearer. The character sequence "1234" is not the binary 32-bit number 0x1234. The text representation is governed by encoding, character order, and parsing rules. Byte-by-byte reversal of a string will change the text, and for a multi-byte encoding it can destroy characters. Even with a numeric field, you must first establish whether it is binary or textual. The reader, accustomed to guessing endian using familiar numbers, runs the risk of starting to correct something that generally belongs to a different class of representations.

Floating-point data introduces a similar boundary. Rearranging the bytes of a bit representation does not perform a floating-point numeric conversion. To exchange such values, the format must specify the width and encoding, and the program must specify a valid way to access the representation. You can’t read arbitrary four bytes as any convenient number just because after BSWAP they resemble the expected pattern. A match on one example establishes too little: positive one, negative zero and special values would require their own exact contract.

What about performance? A single instruction does not establish the cost of a complete parser. First you need to get the data, check the length, define the fields and save the result; the compiler can combine operations differently from the apparent source-code sequence. There is no BSWAP latency measurement or processor comparison in this material. Understanding the instruction is necessary to understand the semantics. A performance decision requires a defined workload and measurements on the relevant system. The word “one” next to an instruction does not replace either the description of the work or its actual cost.

A curious property of permutation can be tested without a processor: two successive reversals of the same width return the original bytes. For our four, after the first it will be 78 56 34 12, after the second again 12 34 56 78. Therefore, the round trip test is useful, but not sufficient. If two functions both incorrectly omit the required conversion, their composition can also return the original bytes. The intermediate representation must also be checked against an independent description of the format. Otherwise, both sides of the program will simply agree on their own mistake.

There is also a case when the error is especially difficult to notice: the value contains identical bytes, for example 0x11111111, or a symmetric sequence. Flipping it around doesn’t change its appearance. A set of such inputs creates a false belief that any transformation is redundant. Therefore, the illustrative quartet deliberately uses different elements. It allows you to distinguish between preserving a value, swapping bytes, and reversing bits. This is a logical test of the example, not a report of editorial tests performed. For a real format, it is useful to have an independent reference for each field, including width, order, and expected raw bytes, so that accidental symmetry does not hide the wrong direction of translation. Then the concept of correctness remains associated with an external contract, and not only with the agreement of two functions with each other.

Sources were checked on November 6, 2026; the Intel SDM volume is dated September 2026. This is an explanation of the existing mechanism, and not an announcement of a new CPU. All byte examples are hypothetical. We got four familiar cards again: 12, 34, 56 and 78. The bytes did not become corrupt along the way and did not themselves need repair. They required a consistent reading rule at the exchange boundary. When this rule is written in the interface, the processor can honestly rearrange the bytes, and the program can save exactly the number that the author of the message wanted to transmit.

Sources & documentation

  1. Intel SDM Volume 2, 325383-093US, September 2026 ↗
  2. Linux man-pages: endian(3) ↗
  3. Linux man-pages: byteorder(3) ↗