Bitset all() function in C++ STL


The bitset all() function an inbuilt function of the C++ STL( Standard Template Library). This function returns a Boolean value. The returned value is true if all the bits of the calling bitset are 1 else it will return false.

The function does not accept any parameter and returns a Boolean value.

Syntax

Bool bitset_name .all()

Sample

Bitset = 100101

Output

false

Because all bits of the set need to be true in order to return a true value.

Example

#include <bits/stdc++.h>
using namespace std;
void printer(bool val){
   if(val){
      cout<< "The bitset has all bits set"<< endl;
   } else{
      cout << "The bitset does not have all bits set"<< endl;
   }
}
int main() {
   bitset<4> bit1(string("1011"));
   bitset<6> bit2(string("111111"));
   cout<<"The bitset is "<<bit1<<endl;
   printer(bit1.all());
   cout<<"The bitset is "<<bit2<<endl;
   printer(bit2.all());
   return 0;
}

Output

The bitset is 1011
The bitset does not have all bits set
The bitset is 111111
The bitset has all bits set
raja
Published on 09-Aug-2019 15:51:03
Advertisements