bswap(3)
πΈ bswap β byte swap, but make it fashion π
okay bestie, gather round, because we're about to talk about the cutest little function family in bswap(3) β it lives in the Library Functions Manual and honestly? it's giving "small but mighty." β¨
β¨ NAME β¨
bswap_16, bswap_32, bswap_64 β byte swap π¦
π LIBRARY π
Standard C library (libc, -lc) β yeah, the classic one, she's everywhere π
π SYNOPSIS π
#include <byteswap.h>
uint16_t bswap_16(uint16_t x);
uint32_t bswap_32(uint32_t x);
uint64_t bswap_64(uint64_t x);
π DESCRIPTION π
so here's the vibe: these functions take a value and return it with the byte order flipped. that's it, that's the whole personality. π
bswap_16flips the bytes of a 2-byte argumentbswap_32flips the bytes of a 4-byte argumentbswap_64flips the bytes of an 8-byte argument
the way that endianness just... reverses? iconic behavior fr fr. byte order reversal is the moment π
β RETURN VALUE β
each one hands back the value of its argument, bytes reversed. same energy, opposite direction. π«Ά
π ERRORS π
ngl these functions always succeed. no errors, no drama, no errno, nothing. they simply do not miss. β¨
π STANDARDS π
GNU. periodt. π
πΈ EXAMPLES πΈ
okay so the program below swaps the bytes of the 8-byte integer you hand it as a command-line argument. here's a whole little shell session showing it off:
$ ./a.out 0x0123456789abcdef;
0x123456789abcdef ==> 0xefcdab8967452301
look at that glow-up. π the bytes literally did a 180 and ate. π₯
π§ Program source π§
here's the source, verbatim, no notes:
#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>\n", argv[0]);
exit(EXIT_FAILURE);
}
x = strtoull(argv[1], NULL, 0);
printf("%#w64x ==> %#w64x\n", x, bswap_64(x));
exit(EXIT_SUCCESS);
}
π¦ SEE ALSO π¦
bestie reminder: if you're out here flipping bytes without checking your endianness first, that's not main character energy, that's just chaos. do a vibe check on your platform, okay? π
and that's the whole page, bestie!! πβ¨
this lil' rewrite came to you from Linux man-pages 6.20, dated 2026-08-10 β like, that's the source and version and date, cute right? π go forth and swap those bytes, you absolute legend. ππΈ
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 2016, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms