Skip to content
Related Articles

Related Articles

bitset any() in C++ STL
  • Last Updated : 12 Nov, 2020

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 any() 

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:  

C++




// C++ 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;
}
Output: 
1100 has a minimum of one-bit set
000000 does not have any bits set




 

Program 2: 

C++




// C++ 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.any();
    if (result2)
        cout << b2 << " has a minimum of one-bit set"
             << endl;
    else
        cout << b2 << " does not have any bit set"
             << endl;
 
    return 0;
}
Output: 
11 has a minimum of one-bit set
000 does not have any bit set




 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :