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