[bit.pow.two]

# 22 General utilities library [[utilities]](./#utilities)

## 22.11 Bit manipulation [[bit]](bit#pow.two)

### 22.11.5 Integral powers of 2 [bit.pow.two]

[ð](#lib:has_single_bit)

`template<class T>
  constexpr bool has_single_bit(T x) noexcept;
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15788)

*Constraints*: T is an unsigned integer type ([[basic.fundamental]](basic.fundamental "6.9.2 Fundamental types"))[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15792)

*Returns*: true if x is an integral power of two;false otherwise[.](#2.sentence-1)

[ð](#lib:bit_ceil)

`template<class T>
  constexpr T bit_ceil(T x);
`

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15806)

Let N be the smallest power of 2 greater than or equal to x[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15809)

*Constraints*: T is an unsigned integer type ([[basic.fundamental]](basic.fundamental "6.9.2 Fundamental types"))[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15813)

*Preconditions*: N is representable as a value of type T[.](#5.sentence-1)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15817)

*Returns*: N[.](#6.sentence-1)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15821)

*Throws*: Nothing[.](#7.sentence-1)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15825)

*Remarks*: A function call expression
that violates the precondition in the *Preconditions*: element
is not a core constant expression ([[expr.const]](expr.const "7.7 Constant expressions"))[.](#8.sentence-1)

[ð](#lib:bit_floor)

`template<class T>
  constexpr T bit_floor(T x) noexcept;
`

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15839)

*Constraints*: T is an unsigned integer type ([[basic.fundamental]](basic.fundamental "6.9.2 Fundamental types"))[.](#9.sentence-1)

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15843)

*Returns*: If x == 0, 0;
otherwise the maximal value y such that has_single_bit(y) is true and y <= x[.](#10.sentence-1)

[ð](#lib:bit_width)

`template<class T>
  constexpr int bit_width(T x) noexcept;
`

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15858)

*Constraints*: T is an unsigned integer type ([[basic.fundamental]](basic.fundamental "6.9.2 Fundamental types"))[.](#11.sentence-1)

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15862)

*Returns*: If x == 0, 0;
otherwise one plus the base-2 logarithm of x,
with any fractional part discarded[.](#12.sentence-1)
