bitset::test()是C++ STL中的一个内置函数,用于测试是否设置了给定索引处的位。
用法:
bitset_name.test(index)
参数:该函数仅接受一个强制性参数索引,该索引指定是否设置该位的索引。
返回值:该函数返回一个布尔值。如果设置了给定索引处的位,则返回true,否则返回false。
以下示例程序旨在说明上述函数:
示例1:
// CPP program to illustrate the
// bitset::test() function
// when bitset is a string
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initialization of bitset
bitset<4> b1(string("1100"));
bitset<6> b2(string("010010"));
// Function to check if 2nd index bit
// is set or not in b1
if (b1.test(2))
cout << "Bit at index 2 of 1100 is set\n";
else
cout << "Bit at index 2 is not set\n";
// Function to check if 3nd index bit
// is set or not in b2
if (b2.test(3))
cout << "Bit at index 3 of 010010 is set\n";
else
cout << "Bit at index 3 of 010010 is not set\n";
return 0;
}
输出:
Bit at index 2 of 1100 is set Bit at index 3 of 010010 is not set
示例2:
// CPP program to illustrate the
// bitset::test() function
// when the bitset is a number
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initialization of bitset
bitset<4> b1(5);
bitset<6> b2(16);
// Function to check if 2nd index bit
// is set or not in b1
if (b1.test(2))
cout << "Bit at index 2 of 5 is set\n";
else
cout << "Bit at index 2 of 5 is not set\n";
// Function to check if 3nd index bit
// is set or not in b2
if (b2.test(3))
cout << "Bit at index 3 of 16 is set\n";
else
cout << "Bit at index 3 of 16 is not set\n";
return 0;
}
输出:
Bit at index 2 of 5 is set Bit at index 3 of 16 is not set
相关用法
- C++ bitset any()用法及代码示例
- C++ bitset none()用法及代码示例
- C++ bitset operator[]用法及代码示例
- C++ bitset set()用法及代码示例
- C++ bitset count()用法及代码示例
- C++ bitset::flip()用法及代码示例
- C++ bitset all()用法及代码示例
- C++ bitset size()用法及代码示例
- C++ bitset reset()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 bitset test() in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。