24 Numerics library [numerics]

24.6 Bit manipulation [bit]

24.6.1 General [bit.general]

The header <bit> provides components to access, manipulate and process both individual bits and bit sequences.

24.6.2 Header <bit> synopsis [bit.syn]

namespace std {
  // [bit.cast], bit_­cast
  template<typename To, typename From>
    constexpr To bit_cast(const From& from) noexcept;

  // [bit.pow.two], integral powers of 2
  template<class T>
    constexpr bool ispow2(T x) noexcept;
  template<class T>
    constexpr T ceil2(T x) noexcept;
  template<class T>
    constexpr T floor2(T x) noexcept;
  template<class T>
    constexpr T log2p1(T x) noexcept;
}

24.6.3 Function template bit_­cast [bit.cast]

template<typename To, typename From> constexpr To bit_cast(const From& from) noexcept;
Returns: An object of type To.
Each bit of the value representation of the result is equal to the corresponding bit in the object representation of from.
Padding bits of the To object are unspecified.
If there is no value of type To corresponding to the value representation produced, the behavior is undefined.
If there are multiple such values, which value is produced is unspecified.
Remarks: This function shall not participate in overload resolution unless:
  • sizeof(To) == sizeof(From) is true;
  • is_­trivially_­copyable_­v<To> is true; and
  • is_­trivially_­copyable_­v<From> is true.
This function shall be constexpr if and only if To, From, and the types of all subobjects of To and From are types T such that:
  • is_­union_­v<T> is false;
  • is_­pointer_­v<T> is false;
  • is_­member_­pointer_­v<T> is false;
  • is_­volatile_­v<T> is false; and
  • T has no non-static data members of reference type.

24.6.4 Integral powers of 2 [bit.pow.two]

template<class T> constexpr bool ispow2(T x) noexcept;
Returns: true if x is an integral power of two; false otherwise.
Remarks: This function shall not participate in overload resolution unless T is an unsigned integer type ([basic.fundamental]).
template<class T> constexpr T ceil2(T x) noexcept;
Returns: The minimal value y such that ispow2(y) is true and y >= x; if y is not representable as a value of type T, the result is an unspecified value.
Remarks: This function shall not participate in overload resolution unless T is an unsigned integer type ([basic.fundamental]).
template<class T> constexpr T floor2(T x) noexcept;
Returns: If x == 0, 0; otherwise the maximal value y such that ispow2(y) is true and y <= x.
Remarks: This function shall not participate in overload resolution unless T is an unsigned integer type ([basic.fundamental]).
template<class T> constexpr T log2p1(T x) noexcept;
Returns: If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any fractional part discarded.
Remarks: This function shall not participate in overload resolution unless T is an unsigned integer type ([basic.fundamental]).