<bit>
函式
標頭 <bit>
包含下列非成員範本函式:
非成員函式 | 說明 |
---|---|
bit_cast |
將物件表示從某個類型重新解譯為另一個類型。 |
bit_ceil |
尋找大於或等於值的兩個最小乘冪。 |
bit_floor |
尋找大於值的兩個最大乘冪。 |
bit_width |
尋找代表值所需的最小位數。 |
countl_zero |
從最顯著位開始,計算設定為零的連續位數目。 |
countl_one |
從最顯著位開始,計算設定為一個的連續位數目。 |
countr_zero |
從最小有效位開始,計算設定為零的連續位數目。 |
countr_one |
從最小有效位開始,計算設定為一個的連續位數目。 |
has_single_bit |
檢查值是否只有一個位設定為一個。 這與測試值是否為兩個乘冪相同。 |
popcount |
計算設定為一個的位數目。 |
rotl |
計算位左旋轉的結果。 |
rotr |
計算位右旋轉的結果。 |
bit_cast
將類型 From
物件的位模式複製到 類型的 To
新物件。
template <class To, class From>
[[nodiscard]] constexpr To bit_cast(const From& from) noexcept;
參數
目標
輸出的類型。
從
所要轉換值的類型。
from
要進行轉換的 值。
傳回值
To
類型的物件。
結果中的每個位都會比對 中對應的位 from
,除非 中有 To
填補位,在此情況下,結果中的位未指定。
範例
#include <bit>
#include <iostream>
int main()
{
float f = std::numeric_limits<float>::infinity();
int i = std::bit_cast<int>(f);
std::cout << "float f = " << std::hex << f
<< "\nstd::bit_cast<int>(f) = " << std::hex << i << '\n';
return 0;
}
float f = inf
std::bit_cast<int>(f) = 7f800000
備註
低階程序代碼通常需要將某個型別的物件解譯為另一種類型。 重新解譯的物件與原始物件具有相同的位表示法,但類型不同。
而不是使用reinterpret_cast
、 或 memcpy()
bit_cast()
,是進行這些轉換的較佳方式。 比較好,因為:
bit_cast()
是constexpr
bit_cast()
需要類型是可簡單複製且大小相同的。 這可防止您使用reinterpret_cast
memcpy
和 遇到的潛在問題,因為它們可以用來不小心且不正確地轉換非簡單可複製的類型。 此外,memcpy()
也可以用來不小心在大小不相同的類型之間複製。 例如,雙精度浮點數(8 個字節)變成不帶正負號的 int(4 個字節),或以相反的方式。
此多載只有在下列情況下才會參與多載解析:
sizeof(To) == sizeof(From)
To
和From
是 is_trivially_copyable。
只有在 、 及其子物件的型別為 時To
,From
這個函式範本constexpr
才會是:
- 不是等位或指標類型
- 不是成員類型的指標
- 非 volatile-qualified
- 沒有屬於參考類型的非靜態數據成員
bit_ceil
尋找大於或等於值的兩個最小乘冪。 例如,假設 3
為 ,會傳 4
回 。
template<class T>
[[nodiscard]] constexpr T bit_ceil(T value);
參數
value
要測試的不帶正負號整數值。
傳回值
兩個大於或等於 value
的最小乘冪。
範例
#include <bit>
#include <bitset>
#include <iostream>
int main()
{
for (auto i = 0u; i < 6u; ++i) // bit_ceil() takes an unsigned integer type
{
auto nextClosestPowerOf2 = std::bit_ceil(i);
std::cout << "\nbit_ceil(0b" << std::bitset<4>(i) << ") = "
<< "0b" << std::bitset<4>(nextClosestPowerOf2);
}
return 0;
}
bit_ceil(0b0000) = 0b0001
bit_ceil(0b0001) = 0b0001
bit_ceil(0b0010) = 0b0010
bit_ceil(0b0011) = 0b0100
bit_ceil(0b0100) = 0b0100
bit_ceil(0b0101) = 0b1000
備註
如果 T
是無符號整數類型,則此範本函式只會參與多載解析。 例如: unsigned int
、 unsigned long
、 unsigned short
、 unsigned char
等。
bit_floor
尋找大於值的兩個最大乘冪。 例如,假設 5
為 ,會傳 4
回 。
template< class T >
[[nodiscard]] constexpr T bit_floor(T value) noexcept;
參數
value
要測試的不帶正負號整數值。
傳回值
兩 value
個大於的最大乘冪。
如果 value
為零,則傳回零。
範例
#include <bit>
#include <bitset>
#include <iostream>
int main()
{
for (auto i = 0u; i < 6u; ++i) // bit_floor() takes an unsigned integer type
{
auto previousPowerOf2 = std::bit_floor(i);
std::cout << "\nbit_floor(0b" << std::bitset<4>(i) << ") = 0b"
<< std::bitset<4>(previousPowerOf2);
}
return 0;
}
bit_floor(0b0000) = 0b0000
bit_floor(0b0001) = 0b0001
bit_floor(0b0010) = 0b0010
bit_floor(0b0011) = 0b0010
bit_floor(0b0100) = 0b0100
bit_floor(0b0101) = 0b0100
備註
如果 T
是無符號整數類型,則此範本函式只會參與多載解析。 例如: unsigned int
、 unsigned long
、 unsigned short
、 unsigned char
等。
bit_width
尋找代表值所需的最小位數。
例如,假設有 5 (0b101),會傳回 3,因為它需要 3 個二進位來表示值 5。
template<class T>
[[nodiscard]] constexpr T bit_width(T value) noexcept;
參數
value
要測試的不帶正負號整數值。
傳回值
表示 value
所需的位數。
如果 value
為零,則傳回零。
範例
#include <bit>
#include <iostream>
int main()
{
for (unsigned i=0u; i <= 8u; ++i)
{
std::cout << "\nbit_width(" << i << ") = "
<< std::bit_width(i);
}
return 0;
}
bit_width(0) = 0
bit_width(1) = 1
bit_width(2) = 2
bit_width(3) = 2
bit_width(4) = 3
bit_width(5) = 3
bit_width(6) = 3
bit_width(7) = 3
bit_width(8) = 4
備註
如果 T
是無符號整數類型,則此範本函式只會參與多載解析。 例如: unsigned int
、 unsigned long
、 unsigned short
、 unsigned char
等。
countl_zero
從最顯著位開始,計算設定為零的連續位數目。
template<class T>
[[nodiscard]] constexpr int countl_zero(T value) noexcept;
參數
value
要測試的不帶正負號整數值。
傳回值
連續零位的數目,從最重要的位開始。
如果 value
為零,則為 型 value
別中的位數。
範例
#include <bit>
#include <bitset>
#include <iostream>
int main()
{
for (unsigned char result = 0, i = 0; i < 9; i++)
{
std::cout << "\ncountl_zero(0b" << std::bitset<8>(result) << ") = " << std::countl_zero(result);
result = result == 0 ? 1 : result * 2;
}
return 0;
}
countl_zero(0b00000000) = 8
countl_zero(0b00000001) = 7
countl_zero(0b00000010) = 6
countl_zero(0b00000100) = 5
countl_zero(0b00001000) = 4
countl_zero(0b00010000) = 3
countl_zero(0b00100000) = 2
countl_zero(0b01000000) = 1
countl_zero(0b10000000) = 0
備註
如果 T
是無符號整數類型,則此範本函式只會參與多載解析。 例如: unsigned int
、 unsigned long
、 unsigned short
、 unsigned char
等。
countl_one
從最顯著位開始,計算設定為一個的連續位數目。
template<class T>
[[nodiscard]] constexpr int countl_one(T value) noexcept;
參數
value
要測試的不帶正負號整數值。
傳回值
連續位數設定為一個,從最重要的位開始。
範例
#include <bit>
#include <bitset>
#include <iostream>
int main()
{
unsigned char value = 0;
for (unsigned char bit = 128; bit > 0; bit /= 2)
{
value |= bit;
std::cout << "\ncountl_one(0b" << std::bitset<8>(value) << ") = "
<< std::countl_one(value);
}
return 0;
}
countl_one(0b10000000) = 1
countl_one(0b11000000) = 2
countl_one(0b11100000) = 3
countl_one(0b11110000) = 4
countl_one(0b11111000) = 5
countl_one(0b11111100) = 6
countl_one(0b11111110) = 7
countl_one(0b11111111) = 8
備註
如果 T
是無符號整數類型,則此範本函式只會參與多載解析。 例如: unsigned int
、 unsigned long
、 unsigned short
、 unsigned char
等。
countr_zero
從最小有效位開始,計算設定為零的連續位數目。
template<class T>
[[nodiscard]] constexpr int countr_zero(T value) noexcept;
參數
value
要測試的不帶正負號整數值。
傳回值
連續零位的數目,從最小有效位開始。
如果 value
為零,則為 型 value
別中的位數。
範例
#include <bit>
#include <bitset>
#include <iostream>
int main()
{
for (unsigned char result = 0, i = 0; i < 9; i++)
{
std::cout << "\ncountr_zero(0b" << std::bitset<8>(result) << ") = "
<< std::countr_zero(result);
result = result == 0 ? 1 : result * 2;
}
return 0;
}
countr_zero(0b00000000) = 8
countr_zero(0b00000001) = 0
countr_zero(0b00000010) = 1
countr_zero(0b00000100) = 2
countr_zero(0b00001000) = 3
countr_zero(0b00010000) = 4
countr_zero(0b00100000) = 5
countr_zero(0b01000000) = 6
countr_zero(0b10000000) = 7
備註
如果 T
是無符號整數類型,則此範本函式只會參與多載解析。 例如: unsigned int
、 unsigned long
、 unsigned short
、 unsigned char
等。
countr_one
從最小有效位開始,計算設定為一個的連續位數目。
template<class T>
[[nodiscard]] constexpr int countr_one(T value) noexcept;
參數
value
要測試的不帶正負號整數值。
傳回值
從最小有效位開始,連續位數設定為一個。
範例
#include <bit>
#include <bitset>
#include <iostream>
int main()
{
unsigned char value = 0;
for (int bit = 1; bit <= 128; bit *= 2)
{
value |= bit;
std::cout << "\ncountr_one(0b" << std::bitset<8>(value) << ") = "
<< std::countr_one(value);
}
return 0;
}
countr_one(0b00000001) = 1
countr_one(0b00000011) = 2
countr_one(0b00000111) = 3
countr_one(0b00001111) = 4
countr_one(0b00011111) = 5
countr_one(0b00111111) = 6
countr_one(0b01111111) = 7
countr_one(0b11111111) = 8
備註
如果 T
是無符號整數類型,則此範本函式只會參與多載解析。 例如: unsigned int
、 unsigned long
、 unsigned short
、 unsigned char
等。
has_single_bit
檢查值是否只有一個位已設定。這與測試值是否為兩個乘冪相同。
template <class T>
[[nodiscard]] constexpr bool has_single_bit(T value) noexcept;
參數
value
要測試的不帶正負號整數值。
傳回值
true
如果 value
只設定了一個位,這也表示 value
是兩個的乘冪。 否則為 false
。
範例
#include <bit>
#include <bitset>
#include <iostream>
#include <iomanip>
int main()
{
for (auto i = 0u; i < 10u; ++i)
{
std::cout << "has_single_bit(0b" << std::bitset<4>(i) << ") = "
<< std::boolalpha << std::has_single_bit(i) << '\n';
}
return 0;
}
has_single_bit(0b0000) = false
has_single_bit(0b0001) = true
has_single_bit(0b0010) = true
has_single_bit(0b0011) = false
has_single_bit(0b0100) = true
has_single_bit(0b0101) = false
has_single_bit(0b0110) = false
has_single_bit(0b0111) = false
has_single_bit(0b1000) = true
has_single_bit(0b1001) = false
備註
如果 T
是無符號整數類型,則此範本函式只會參與多載解析。 例如: unsigned int
、 unsigned long
、 unsigned short
、 unsigned char
等。
popcount
計算未帶正負號整數值中設定為一個位的數目。
template<class T>
[[nodiscard]] constexpr int popcount(T value) noexcept;
參數
value
要測試的不帶正負號整數值。
傳回值
中的數位位設定為一個 value
。
範例
#include <bit>
#include <bitset>
#include <iostream>
int main()
{
for (unsigned char value = 0; value < 16; value++)
{
std::cout << "\npopcount(0b" << std::bitset<4>(value) << ") = "
<< std::popcount(value);
}
return 0;
}
popcount(0b0000) = 0
popcount(0b0001) = 1
popcount(0b0010) = 1
popcount(0b0011) = 2
popcount(0b0100) = 1
popcount(0b0101) = 2
popcount(0b0110) = 2
popcount(0b0111) = 3
popcount(0b1000) = 1
popcount(0b1001) = 2
popcount(0b1010) = 2
popcount(0b1011) = 3
popcount(0b1100) = 2
popcount(0b1101) = 3
popcount(0b1110) = 3
popcount(0b1111) = 4
備註
如果 T
是無符號整數類型,則此範本函式只會參與多載解析。 例如: unsigned int
、 unsigned long
、 unsigned short
、 unsigned char
等。
rotl
旋轉不帶正負號整數值的位,以離開指定的次數。 最左邊位「掉落」的位會旋轉到最右邊的位。
template<class T>
[[nodiscard]] constexpr T rotl(T value, int s) noexcept;
參數
value
要旋轉的不帶正負號整數值。
s
要執行的左旋轉數目。
傳回值
向左旋轉 value
的結果, s
時間。
如果 s
為零,則傳 value
回 。
如果 s
為負數,則為 rotr(value, -s)
。 最右邊位「掉下來」的位會旋轉到最左邊的位。
範例
#include <bit>
#include <bitset>
#include <iostream>
int main()
{
unsigned char bits = 1;
for (int i = 0; i < 8; ++i)
{
std::cout << "rotl(0b" << std::bitset<8>(bits) << ", 1) = ";
bits = std::rotl(bits, 1);
std::cout << "0b" << std::bitset<8>(bits) << '\n';
}
std::cout << "rotl(0b" << std::bitset<8>(bits) << ",-1) = ";
bits = std::rotl(bits, -1);
std::cout << "0b" << std::bitset<8>(bits);
return 0;
}
rotl(0b00000001, 1) = 0b00000010
rotl(0b00000010, 1) = 0b00000100
rotl(0b00000100, 1) = 0b00001000
rotl(0b00001000, 1) = 0b00010000
rotl(0b00010000, 1) = 0b00100000
rotl(0b00100000, 1) = 0b01000000
rotl(0b01000000, 1) = 0b10000000
rotl(0b10000000, 1) = 0b00000001
rotl(0b00000001,-1) = 0b10000000
備註
如果 T
是無符號整數類型,則此範本函式只會參與多載解析。 例如: unsigned int
、 unsigned long
、 unsigned short
、 unsigned char
等。
rotr
旋轉指定次數右邊的 value
位。 最右邊位「掉下來」的位會旋轉回最左邊的位。
template<class T>
[[nodiscard]] constexpr T rotr(T value, int s) noexcept;
參數
value
要旋轉的不帶正負號整數值。
s
要執行的右旋轉數目。
傳回值
向右s
旋轉value
時間的結果。
如果 s
為零,則傳 value
回 。
如果 s
為負數,則為 rotl(value, -s)
。 最左邊位「掉下來」的位會旋轉回最右邊的位。
範例
#include <bit>
#include <bitset>
#include <iostream>
int main()
{
unsigned char bits = 128;
for (int i = 0; i < 8; ++i)
{
std::cout << "rotr(0b" << std::bitset<8>(bits) << ", 1) = ";
bits = std::rotr(bits, 1);
std::cout << "0b" << std::bitset<8>(bits) << '\n';
}
std::cout << "rotr(0b" << std::bitset<8>(bits) << ",-1) = ";
bits = std::rotr(bits, -1);
std::cout << "0b" << std::bitset<8>(bits);
return 0;
}
rotr(0b10000000, 1) = 0b01000000
rotr(0b01000000, 1) = 0b00100000
rotr(0b00100000, 1) = 0b00010000
rotr(0b00010000, 1) = 0b00001000
rotr(0b00001000, 1) = 0b00000100
rotr(0b00000100, 1) = 0b00000010
rotr(0b00000010, 1) = 0b00000001
rotr(0b00000001, 1) = 0b10000000
rotr(0b10000000,-1) = 0b00000001
備註
如果 T
是無符號整數類型,則此範本函式只會參與多載解析。 例如: unsigned int
、 unsigned long
、 unsigned short
、 unsigned char
等。
需求
標頭: <bit>
命名空間:std
/std:c++20
或更新版本為必要專案。