Swap the bytes of a value
A byte swap reverses the order of the bytes of a value.
The 16-bit value 0x1234 becomes 0x3412.
A swap converts a value between big endian and little endian.
Try a byte swap in the bit editor
The value 0x1234 in a 16-bit register. Edit the expression or select a bit to flip it.
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
0x1234
hex · 16 bits
- value
- 4660
Bit width
Byte order
A byte swap step by step
Reverse the order of the bytes of the value: The steps follow the value above.
- Write the value as bytes: 12 34
- Reverse the order of the bytes.
- The result is 34 12.
- In hex, 0x1234 becomes 0x3412.
A value before and after a swap
| Bits | Value | After the swap |
|---|---|---|
| 16 | 0x1234 | 0x3412 |
| 32 | 0x12345678 | 0x78563412 |
| 64 | 0x0102030405060708 | 0x0807060504030201 |
Where a byte swap is used
A network protocol sends the high byte first, and most processors hold it last.
A file written on one target and read on another needs a swap.
A register read over a link arrives byte by byte, so the order decides the value.
A byte swap: points to note
- A swap of a single byte changes nothing. The order matters at a bit width of 16 or more.
- A swap is not a bit reverse. The bits inside each byte keep their order.
- The C functions are htons and htonl for a network order, and __builtin_bswap16 and its wider forms.
- Beetwise holds a big endian and a little endian view of the same bits, so the toggle shows both.
The full bit editor
The full editor holds many values, evaluates expressions across them, and reads live values from a serial port.
Open this value in the full editorQuestions about a byte swap
- What is 0x1234 after a byte swap?
- The result is 0x3412. The two bytes change places and the bits inside each byte stay.
- Which order does a network use?
- A network protocol uses big endian, which the standards call network byte order.
- Does a byte swap change the bits?
- It moves whole bytes. Each byte keeps its own bit order.