Files
cppdraft_translate/cppdraft/bit/pow/two.md
2025-10-25 03:02:53 +03:00

109 lines
3.2 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[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.2Fundamental 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.2Fundamental 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.7Constant 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.2Fundamental 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.2Fundamental 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)