| 1 |
.\" Copyright 2016, Michael Kerrisk <[email protected]> |
| 2 |
.\" Copyright, the authors of the Linux man-pages project |
| 3 |
.\" |
| 4 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 5 |
.\" |
| 6 |
.TH bswap 3 2026-02-16 "Linux man-pages 6.18" |
| 7 |
.SH NAME |
| 8 |
bswap_16, bswap_32, bswap_64 \- reverse order of bytes |
| 9 |
.SH LIBRARY |
| 10 |
Standard C library |
| 11 |
.RI ( libc ,\~ \-lc ) |
| 12 |
.SH SYNOPSIS |
| 13 |
.nf |
| 14 |
.B #include <byteswap.h> |
| 15 |
.P |
| 16 |
.BI "uint16_t bswap_16(uint16_t " x ); |
| 17 |
.BI "uint32_t bswap_32(uint32_t " x ); |
| 18 |
.BI "uint64_t bswap_64(uint64_t " x ); |
| 19 |
.fi |
| 20 |
.SH DESCRIPTION |
| 21 |
These functions return a value in which the order of the bytes |
| 22 |
in their 2-, 4-, or 8-byte arguments is reversed. |
| 23 |
.SH RETURN VALUE |
| 24 |
These functions return the value of their argument with the bytes reversed. |
| 25 |
.SH ERRORS |
| 26 |
These functions always succeed. |
| 27 |
.SH STANDARDS |
| 28 |
GNU. |
| 29 |
.SH EXAMPLES |
| 30 |
The program below swaps the bytes of the 8-byte integer supplied as |
| 31 |
its command-line argument. |
| 32 |
The following shell session demonstrates the use of the program: |
| 33 |
.P |
| 34 |
.in +4n |
| 35 |
.EX |
| 36 |
.RB $ " ./a.out 0x0123456789abcdef" ; |
| 37 |
0x123456789abcdef ==> 0xefcdab8967452301 |
| 38 |
.EE |
| 39 |
.in |
| 40 |
.SS Program source |
| 41 |
\& |
| 42 |
.\" SRC BEGIN (bswap.c) |
| 43 |
.EX |
| 44 |
#include <byteswap.h> |
| 45 |
#include <inttypes.h> |
| 46 |
#include <stdint.h> |
| 47 |
#include <stdio.h> |
| 48 |
#include <stdlib.h> |
| 49 |
\& |
| 50 |
int |
| 51 |
main(int argc, char *argv[]) |
| 52 |
{ |
| 53 |
uint64_t x; |
| 54 |
\& |
| 55 |
if (argc != 2) { |
| 56 |
fprintf(stderr, "Usage: %s <num>\[rs]n", argv[0]); |
| 57 |
exit(EXIT_FAILURE); |
| 58 |
} |
| 59 |
\& |
| 60 |
x = strtoull(argv[1], NULL, 0); |
| 61 |
printf("%#w64x ==> %#w64x\[rs]n", x, bswap_64(x)); |
| 62 |
\& |
| 63 |
exit(EXIT_SUCCESS); |
| 64 |
} |
| 65 |
.EE |
| 66 |
.\" SRC END |
| 67 |
.SH SEE ALSO |
| 68 |
.BR byteorder (3), |
| 69 |
.BR endian (3) |