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.
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
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.
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
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_castpunning. Reading afloatthrough auint32_t*violates strict aliasing;bit_cast(ormemcpy) does not. The compiler turns it into a register move. constexprfriendly:bit_castandbyteswapwork 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::endianinstead of testing an address of a union. In C++20 mode GCC reportsstd::endian::nativeaslittleon x86 and Arm. byteswapdoes 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_bswap32orhtonl.