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_16 flips the bytes of a 2-byte argument
  • bswap_32 flips the bytes of a 4-byte argument
  • bswap_64 flips 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms