C++ Bitset Library - test() Function



Description

The C++ function std::bitset::test() Tests whether Nth bit is set or not.

Declaration

Following is the declaration for std::bitset::test() function form std::bitset header.

C++98

 bool test (size_t pos) const; 

Parameters

None

Return value

Returns true if Nth bit is set otherwise false.

Exceptions

Throws out_of_range exception if pos is greater than or equal to bitset size.

Example

The following example shows the usage of std::bitset::test() function.

 #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; } 

Let us compile and run the above program, this will produce the following result −

 1st bit is set. 0th bit is not set. 
bitset.htm
Advertisements