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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
bitset<4> b1(string( "1100" ));
bitset<6> b2(string( "000000" ));
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;
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
bitset<2> b1(3);
bitset<3> b2(0);
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;
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
Share your thoughts in the comments
Please Login to comment...