Bit Manipulation
Learn how to manipulate bits with the '<bit>' header.
We'll cover the following...
The header <bit>
supports functions to access and manipulate individual bits or bit sequences.
std::endian
Thanks to the new type std::endian
, you get the endianness of a scalar type. Endianness can be big-endian or little-endian. Big-endian means that the most significant byte is the furthest left. Little-endian means that the least significant byte is furthest left. A scalar type is either an arithmetic type, an enum
, a pointer, a member pointer, or a std::nullptr_t
. The class endian
provides the endianness of all scalar types:
Press + to interact
enum class endian{little = /*implementation-defined*/,big = /*implementation-defined*/,native = /*implementation-defined*/};
- If all scalar types are little-endian,
std::endian::native
is equal tostd::endian::little
. - If all scalar types are big-endian,
std::endian::native
is equal tostd::endian::big
. Even corner cases are supported: - If all scalar types have a size of 1 and therefore endianness does not matter, the values of the enumerators
std::endian::little
,std::endian::big
, andstd::endian::native
are identical. - If the platform uses mixed endianness,
std::endian::native
is neither equal tostd::endian::big
norstd::endian::little
.
When I run the following program on an ...