Open In App

bitset any() in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

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




 



Last Updated : 12 Nov, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads