bitset::test() is an inbuilt function in C++ STL which tests whether the bit at a given index is set or not.
Syntax:
bitset_name.test(index)
Parameters: The function accepts only a single mandatory parameter index which specifies the index at which the bit is set or not.
Return Value: The function returns a boolean value. It returns true if the bit at the given index is set else it returns false.
Below programs illustrate the above function:
Program 1:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
bitset<4> b1(string("1100"));
bitset<6> b2(string("010010"));
if (b1.test(2))
cout << "Bit at index 2 of 1100 is set\n";
else
cout << "Bit at index 2 is not set\n";
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;
}
|
Output:
Bit at index 2 of 1100 is set
Bit at index 3 of 010010 is not set
Program 2:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
bitset<4> b1(5);
bitset<6> b2(16);
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";
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;
}
|
Output:
Bit at index 2 of 5 is set
Bit at index 3 of 16 is not set
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...