Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2014-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_COMPAT_BYTESWAP_H6#define BITCOIN_COMPAT_BYTESWAP_H8#include <cstdint>9#ifdef _MSC_VER10#include <cstdlib>11#endif14// All internal_bswap_* functions can be replaced with std::byteswap once we15// require c++23. Both libstdc++ and libc++ implement std::byteswap via these16// builtins.18#ifndef DISABLE_BUILTIN_BSWAPS19# if defined __has_builtin20# if __has_builtin(__builtin_bswap16)21# define bitcoin_builtin_bswap16(x) __builtin_bswap16(x)22# endif23# if __has_builtin(__builtin_bswap32)24# define bitcoin_builtin_bswap32(x) __builtin_bswap32(x)25# endif26# if __has_builtin(__builtin_bswap64)27# define bitcoin_builtin_bswap64(x) __builtin_bswap64(x)28# endif29# elif defined(_MSC_VER)30# define bitcoin_builtin_bswap16(x) _byteswap_ushort(x)31# define bitcoin_builtin_bswap32(x) _byteswap_ulong(x)32# define bitcoin_builtin_bswap64(x) _byteswap_uint64(x)33# endif34#endif36// MSVC's _byteswap_* functions are not constexpr38#ifndef _MSC_VER39#define BSWAP_CONSTEXPR constexpr40#else41#define BSWAP_CONSTEXPR42#endif44inline BSWAP_CONSTEXPR uint16_t internal_bswap_16(uint16_t x)45{46#ifdef bitcoin_builtin_bswap1647return bitcoin_builtin_bswap16(x);48#else49return (x >> 8) | (x << 8);50#endif51}53inline BSWAP_CONSTEXPR uint32_t internal_bswap_32(uint32_t x)54{55#ifdef bitcoin_builtin_bswap3256return bitcoin_builtin_bswap32(x);57#else58return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |59((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));60#endif61}63inline BSWAP_CONSTEXPR uint64_t internal_bswap_64(uint64_t x)64{65#ifdef bitcoin_builtin_bswap6466return bitcoin_builtin_bswap64(x);67#else68return (((x & 0xff00000000000000ull) >> 56)69| ((x & 0x00ff000000000000ull) >> 40)70| ((x & 0x0000ff0000000000ull) >> 24)71| ((x & 0x000000ff00000000ull) >> 8)72| ((x & 0x00000000ff000000ull) << 8)73| ((x & 0x0000000000ff0000ull) << 24)74| ((x & 0x000000000000ff00ull) << 40)75| ((x & 0x00000000000000ffull) << 56));76#endif77}79#endif // BITCOIN_COMPAT_BYTESWAP_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.