Updated June 21, 2023
Introduction to C++ test() Function
The test() function in C++ is used to test whether in a bit string at the specified index the bit is set or not. The test() function is a built-in function in C++ which is defined in the <bits/stdc++.h> or <bitset> header file, this header file includes every standard library. The text() function accepts only one parameter which is the index position of the bit string, at that index position if the bit is one then the function returns true, otherwise returns false if the bit is zero.
Syntax:
bool test(int index) ;
Parameters of C++ test() Function
Index: index is an int parameter that specifies the index position at which it is to test whether the bit is set or not. This is not an optional parameter.
The return value of this function is a boolean type, if the bit is set at the given index position then return true otherwise return false if the bit is not set.
Working of test() function in C++
The test() function is used or call on the bitset sting(a collection of 0’s and 1’s stores in the string format) to find at a particular index position in a bit string whether the bit is set(1) or not set(0), so the test() function accepts only one parameter that is the index position of the bit string and checks for that index position in a bit string the bit is 1 or 0. If the bit store is 1 then returns true, else returns false if the bit is 0, as we can see in the below examples.
Examples to Implement test() Function in C++
Below are the examples of test() function:
Example #1
We write the C++ code to understand the test() function more clearly with the following example where we use test() function to check all the bit of the bit string, as below:
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main ()
{
int i;
// Initialization of bitset
bitset<6> bstr(string("010101"));
// code to check all the bits whether set or not
for(i=0; i<6; i++)
{
cout << "The bit at index "<< i << " is "<< bstr.test(i) << endl;
}
return 0;
}
Output:
As in the above code the test() function is used to check all the bit with the help of for loop to get the index values. The index values 0 to 5 is passing to the test() function, so in each loop the particular index function is checking whether the bit is set that is one or not set that is zero. Therefore as in output, we can see that it is printing all the bits whether set or not from right to left.
Example #2
We write the C++ code to understand the test() function where we use test() function to check the user given bit index of the bit string, as below:
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main ()
{
int i, index;
// Initialization of bitset
bitset<6> bstr(string("010101"));
cout<<"Enter the bit index, which you want to test :";
cin>>index;
// code to check whether the bit at given index is set or not
cout << "The bit at given index "<< index << " is "<< bstr.test(index) << endl;
return 0;
}
Output:
As in the above code the index position is accepted by the user and passed to the test() function to check only the given index bit is set or not set. As the user passed the index value 3 which is farther passed to the test() function, in the bit string (“010101”) we can see that at index 3 the bit is 0 which means not set. So in output we can see that it is printing the bit at given index is 0.
Example #3
We write the C++ code to understand the test() function where we use test () function to check the user given bit index whether set or not, as below:
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main ()
{
int index;
// Initialization of bitset
bitset<6> bstr(string("010101"));
cout<<"Enter the bit index, which you want to test :";
cin>>index;
// code to check whether the bit at given index is set or not
if(bstr.test(index)){
cout << "The bitset is set at index " << index;
}
else
{
cout << "The bitset is not set at index " << index;
}
return 0;
}
Output:
As in the above code the index position is accepted by the user and passed the test() function. As the user passed the index value 3 which is farther passed to the test() function, as in the bit string (“010101”) we can see that at index 3 the bit is 0 which means not set, so the test() function return false and therefor in the output the false statement is printing.
Another output of the above code when the user given an input as 4 is:
Example #4
We write the C++ code to understand the test() function where we use test() function to compare two different strings, as below:
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main ()
{
int i;
// Initialization of bitset
bitset<6> bstr1(string("010101"));
bitset<6> bstr2(string("011001"));
// code to check whether the two bit strings are equal or not
for( i=0; i<6; i++)
{
if(bstr1.test(i) == bstr2.test(i)){
continue; }
else {
break; }
}
if( i == 6 ) {
cout<< "Both the bit strings are equal.";
}
else {
cout<< "Both the bit strings are not equal.";
}
return 0;
}
Output:
As in the above code the test() function is used to compare two bit strings, bit by bit. Here the both string bits are not same, so the output is printing that both bit strings are not equal.
Conclusion
The test() function in C++ is a built in function which can be used to test whether at a given index the bit is set or not. The test() function is defined in the <bits/stdc++.h> or <bitset> header files.
Recommended Articles
This is a guide to C++ test(). Here we discuss the brief overview of C++ test() Function and its working along with Examples and Code Implementation. You can also go through our other suggested articles to learn more –