bitset any() in C++ STL

The bitset::any() is an inbuilt function in C++ STL which returns True if at least one bit is set in a number. It returns False if all the bits are not set or if the number is zero.

Syntax:

bool count() 

Parameter: The function does not accepts any parameter.

Return Value: The function returns a boolean value. The boolean value thus returned is True if any of its bits are set. It is False if none of its bits are set.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate the
// bitset::any() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialization
    bitset<4> b1(string("1100"));
    bitset<6> b2(string("000000"));
  
    // function to check if any of
    // its bits are set or not
    bool result1 = b1.any();
    if (result1)
        cout << b1 << " has a minimum of one-bit set"
             << endl;
    else
        cout << b1 << " does not have any bits set"
             << endl;
  
    // function to check if any of
    // its bits are set or not
    bool result2 = b2.any();
    if (result2)
        cout << b2 << "  has a minimum of one-bit set"
             << endl;
    else
        cout << b2 << " does not have any bits set"
             << endl;
  
    return 0;
}

chevron_right


Output:

1100 has a minimum of one-bit set
000000 does not have any bits set

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

11 has a minimum of one-bit set
000 does not have any bit set


My Personal Notes arrow_drop_up


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.