The bitset::any()
function in C++ is used to find if any of the bits available in the bitset
object are set or not. This function is available in the bitset
header file.
The syntax of the bitset::any()
function is shown below:
bool any();
The function does not accept any parameters.
The function returns a boolean value, where true
denotes if any of the bits are set, and false
indicates that none are set.
Let’s have a look at the code:
#include <iostream>#include <bitset>using namespace std;int main() {bitset<4> b1 = 10;if (b1.any())cout << "Some bits are set in " << b1;elsecout << "None of the bits are not set in " << b1;return 0;}
In lines 1 and 2, we import the required header files.
In line 6, we create a bitset
object with the capacity to store 4 bits. This object will contain the binary representation of the integer 10.
In line 8, we call the any()
function and print if any of the bits are set.
If none of the bits are set, then, in line 11, we print the corresponding message.
In this way, we can use the bitset::any()
function to check if any of the bits are set or not.