bitset all() function in C++ STL

The bitset::all() is a built-in function in C++ STL which returns True if all bits are set in the binary representation of a number if it is initialized in the bitset. It returns False if all the bits are not set.

Syntax:

bool bitset_name.all() 

Parameter: This function does not accepts any parameter.



Return Value: The function returns a boolean value. The boolean value returned is true if all the bits are set, else the returned value is false,

Below programs illustrates the bitset::all() function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate the
// bitset::all() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Initialization of bitset
    bitset<4> b1(string("1100"));
    bitset<6> b2(string("111111"));
  
    // Function that checks if all
    // the bits are set or not
    bool result1 = b1.all();
    if (result1)
        cout << b1 << " has all bits set"
             << endl;
    else
        cout << b1 << " does not have all bits set"
             << endl;
  
    // Function that checks if all
    // the bits are set or not
    bool result2 = b2.all();
    if (result2)
        cout << b2 << " has all bits set"
             << endl;
    else
        cout << b2 << " does not have all bits set"
             << endl;
  
    return 0;
}

chevron_right


Output:

1100 does not have all bits set
111111 has all bits set

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate the
// bitset::all() function
// when the input is as a number
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Initialization of bitset
    bitset<2> b1(3);
    bitset<3> b2(5);
  
    // Function that checks if all
    // the bits are set or not
    bool result1 = b1.all();
    if (result1)
        cout << b1 << " has all bits set"
             << endl;
    else
        cout << b1 << " does not have all bits set"
             << endl;
  
    // Function that checks if all
    // the bits are set or not
    bool result2 = b2.all();
    if (result2)
        cout << b2 << " has all bits set"
             << endl;
    else
        cout << b2 << " does not have all bits set"
             << endl;
  
    return 0;
}

chevron_right


Output:

11 has all bits set
101 does not have all bits set


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.