Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Memory and Utilities

C++ std::byteswap and std::bit_cast

Reference for std::byteswap (C++23), std::bit_cast (C++20) and the <bit> helpers popcount, countl_zero, rotl and endian for safe bit-level work.

The <bit> header (C++20, with byteswap added in C++23) gives names to bit-level operations that used to need compiler intrinsics or undefined tricks. std::byteswap(x) reverses the bytes of an integer, which converts between little-endian and big-endian. std::bit_cast<To>(from) reinterprets the object representation of one trivially copyable value as another type of the same size, the well-defined replacement for pointer casting and union type punning.

The same header contains std::popcount, std::countl_zero, std::countr_zero, std::bit_width, std::has_single_bit, std::rotl, std::rotr and the enumeration std::endian.

C++
std::uint32_t big = std::byteswap(std::uint32_t{0x11223344});   // 0x44332211
float f = std::bit_cast<float>(std::uint32_t{0x3f800000});     // 1.0f

Try it

C++ byteswap and endianness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ bit_cast, popcount and rotations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<std::integral T> constexpr T byteswap(T value) noexcept;                  // C++23

template<class To, class From>
constexpr To bit_cast(const From& from) noexcept;                                 // C++20
// requires sizeof(To) == sizeof(From) and both trivially copyable

enum class endian { little = /*...*/, big = /*...*/, native = /*...*/ };           // C++20
Name Description
byteswap(value) Reverses the byte order of an integer type. For a 1-byte type it returns the value unchanged.
bit_cast<To>(from) Creates a To whose bytes are copied from from.
popcount, countl_zero, countr_zero, bit_width Counting functions for unsigned integers only.
rotl(x, s), rotr(x, s) Rotate the bits left or right by s; negative s rotates the other way.
bit_ceil, bit_floor, has_single_bit Powers-of-two helpers.

Errors / undefined behavior: bit_cast is a compile error if the sizes differ or either type is not trivially copyable. The result is undefined if it would produce a value that is not valid for To (for example a bool with a byte other than 0 or 1). bit_ceil is undefined when the result does not fit in the type.

Usage notes

  • Replaces reinterpret_cast punning. Reading a float through a uint32_t* violates strict aliasing; bit_cast (or memcpy) does not. The compiler turns it into a register move.
  • constexpr friendly: bit_cast and byteswap work in constant evaluation, except when the types contain pointers, unions or references.
  • Float layout is the platform's. Most machines use IEEE 754, but the standard does not promise it, so results of bit_cast<uint32_t>(float) carry that assumption.
  • The bit helpers reject signed types at compile time. Cast to the unsigned type first.
  • Use std::endian instead of testing an address of a union. In C++20 mode GCC reports std::endian::native as little on x86 and Arm.
  • byteswap does not move data in memory. For arrays, swap each element, or use a serialization format with fixed order.
  • Before C++23 the same job was done with __builtin_bswap32 or htonl.