std::byteswap - cppreference.com (original) (raw)

| Defined in header | | | | -------------------------------------------------------------- | | ------------- | | template< class T > constexpr T byteswap( T n ) noexcept; | | (since C++23) |

Reverses the bytes in the given integer value n.

std::byteswap participates in overload resolution only if T satisfies integral, i.e., T is an integer type. The program is ill-formed if T has padding bits.

Contents

[edit] Parameters

[edit] Return value

An integer value of type T whose object representation comprises the bytes of that of n in reversed order.

[edit] Notes

This function is useful for processing data of different endianness.

Feature-test macro Value Std Feature
__cpp_lib_byteswap 202110L (C++23) std::byteswap

[edit] Possible implementation

[edit] Example

Possible output:

byteswap for U16: CAFE : FE CA FECA : CA FE   byteswap for U32: DEADBEEF : EF BE AD DE EFBEADDE : DE AD BE EF   byteswap for U64: 0123456789ABCDEF : EF CD AB 89 67 45 23 01 EFCDAB8967452301 : 01 23 45 67 89 AB CD EF

[edit] See also

| | indicates the endianness of scalar types (enum) [edit] | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | | computes the result of bitwise left-rotation (function template) [edit] | | | computes the result of bitwise right-rotation (function template) [edit] |