Works of test() function in C++

Works of test() function in C++
Techiio-author
Written by Sagar RabidasFebruary 12, 2022
7 min read
C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss the Working of the test() function in C++

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 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.

Syntax:

bool test(int index) ;

Working of test() function in C++

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.

Examples to Implement of test() Function in C++

Below are the examples of the test() function:

Example #1

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:

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

Example #2

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:

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

Example #3

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:

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

Example #4

We write the C++ code to understand the test() function where we use the 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;
}

C
C++
C++ functions
test() function
test() function C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance