Bitset Test Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Bitset Test Function

Bitset Test Function

BLUF: Mastering Bitset Test Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Bitset Test Function

C++ is renowned for its efficiency. Learn how Bitset Test Function enables low-level control and high-performance computing in the tutorial below.

The test function in C++ with bitset is employed to check if the bit at a specific position, p, is set. It will return either true or false based on whether the bit at position p is set or not.

Syntax

Example

bool test(int p);

Parameter

It accepts a single parameter indicating the position where the bit should be toggled.

Return value

It returns a boolean value of true if the bit at position p is turned on, and false if it is turned off.

Example 1

Example

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
    bitset<8> b(string("01010110"));
    bool a=b.test(3);
    bool c=b.test(2);
     cout << " bitset b is set at position 3 : " << a <<'\n';
     cout << " bitset b is set at position 2 : " << c <<'\n';
   return 0;
}

Output:

Output

bitset b is set at position 3 : 0
 bitset b is set at position 2 : 1

Example 2

Example

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
    bitset<8> b(string("01010110"));
    bitset<4> b1;
     cout << " bitset b is set at position 3 : " << b.test(3) <<'\n';
     cout << " bitset b1 is set at position 2 : " << b.test(2) <<'\n';
   return 0;
}

Output:

Output

bitset b is set at position 3 : 0
bitset b1 is set at position 2 : 1

Example 3

Example

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
    bitset<8> b(string("01010110"));
    if(b.test(1)){
     cout << "bitset is set at postion 1";
    }
    else
    {
        cout << "bitset is not set at position 1";
    }
   return 0;
}

Output:

Output

bitset is set at postion 1

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience