Software Developer
The test() function in C++ is used to test whether in a bit string at the specified index the bit is set or now 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 the handiest one parameter which is the index role of the bit string, at that index function if the bit is one then the function returns authentic, otherwise returns false if the bit is zero.
bool test(int index) ;
The test() function is used or known as on the bitset string(a set of 0’s and 1’s stores in the string layout) to locate at a selected index function in a piece string whether the bit is set(1) or not set(0), so the take a look at() function accepts best one parameter that is the index function of the bit string and checks for that index function in a piece string the bit is 1 or 0. If the bit keep is 1 then returns authentic, else returns false if the bit is 0, as we will see in the underneath examples.
Below are the examples of the test() function:
We write the C++ code to understand the test() function more clearly with the following example where we use the test() function to check all the bits of the bit string, as below:
#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;
}
We write the C++ code to understand the test() function where we use the test() function to check the user given bit index of the bit string, as below:
#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;
}
We write the C++ code to understand the test() function where we use the test () function to check the user given bit index whether set or not, as below:
#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;
}
We write the C++ code to understand the test() function where we use the test() function to compare two different strings, as below:
#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;
}