C++ Bitset 库 - test() 函数
描述
C++ 函数 std::bitset::test() 测试是否设置了 Nth 位。
声明
以下是 std::bitset::test() 函数形式 std::bitset 头的声明。
C++98
bool test (size_t pos) const;
参数
None
返回值
如果设置了 Nth 位,则返回 true,否则返回 false。
异常
如果 pos 大于或等于 bitset 大小,则抛出 out_of_range 异常。
示例
以下示例显示了 std::bitset::test() 函数的用法。
#include <iostream>
#include <bitset>
using namespace std;
int main(void) {
bitset<4> b(1010);
if (b.test(1))
cout << "1st bit is set." << endl;
if (!b.test(0))
cout << "0th bit is not set." << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
1st bit is set. 0th bit is not set.

