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_ENDIAN_H6#define BITCOIN_COMPAT_ENDIAN_H8#include <compat/byteswap.h>10#include <bit>11#include <cstdint>13inline BSWAP_CONSTEXPR uint16_t htobe16_internal(uint16_t host_16bits)14{15if constexpr (std::endian::native == std::endian::little) return internal_bswap_16(host_16bits);16else return host_16bits;17}18inline BSWAP_CONSTEXPR uint16_t htole16_internal(uint16_t host_16bits)19{20if constexpr (std::endian::native == std::endian::big) return internal_bswap_16(host_16bits);21else return host_16bits;22}23inline BSWAP_CONSTEXPR uint16_t be16toh_internal(uint16_t big_endian_16bits)24{25if constexpr (std::endian::native == std::endian::little) return internal_bswap_16(big_endian_16bits);26else return big_endian_16bits;27}28inline BSWAP_CONSTEXPR uint16_t le16toh_internal(uint16_t little_endian_16bits)29{30if constexpr (std::endian::native == std::endian::big) return internal_bswap_16(little_endian_16bits);31else return little_endian_16bits;32}33inline BSWAP_CONSTEXPR uint32_t htobe32_internal(uint32_t host_32bits)34{35if constexpr (std::endian::native == std::endian::little) return internal_bswap_32(host_32bits);36else return host_32bits;37}38inline BSWAP_CONSTEXPR uint32_t htole32_internal(uint32_t host_32bits)39{40if constexpr (std::endian::native == std::endian::big) return internal_bswap_32(host_32bits);41else return host_32bits;42}43inline BSWAP_CONSTEXPR uint32_t be32toh_internal(uint32_t big_endian_32bits)44{45if constexpr (std::endian::native == std::endian::little) return internal_bswap_32(big_endian_32bits);46else return big_endian_32bits;47}48inline BSWAP_CONSTEXPR uint32_t le32toh_internal(uint32_t little_endian_32bits)49{50if constexpr (std::endian::native == std::endian::big) return internal_bswap_32(little_endian_32bits);51else return little_endian_32bits;52}53inline BSWAP_CONSTEXPR uint64_t htobe64_internal(uint64_t host_64bits)54{55if constexpr (std::endian::native == std::endian::little) return internal_bswap_64(host_64bits);56else return host_64bits;57}58inline BSWAP_CONSTEXPR uint64_t htole64_internal(uint64_t host_64bits)59{60if constexpr (std::endian::native == std::endian::big) return internal_bswap_64(host_64bits);61else return host_64bits;62}63inline BSWAP_CONSTEXPR uint64_t be64toh_internal(uint64_t big_endian_64bits)64{65if constexpr (std::endian::native == std::endian::little) return internal_bswap_64(big_endian_64bits);66else return big_endian_64bits;67}68inline BSWAP_CONSTEXPR uint64_t le64toh_internal(uint64_t little_endian_64bits)69{70if constexpr (std::endian::native == std::endian::big) return internal_bswap_64(little_endian_64bits);71else return little_endian_64bits;72}74#endif // BITCOIN_COMPAT_ENDIAN_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.