What is the bitset::all() function in C++?

The bitset::all() function in C++ is used to find whether all the bits available in the bitset object are set. This function is available in the bitset header file.

Syntax

The syntax of the bitset::all() function is shown below.

bool all();

Parameters

The function does not accept any parameters.

Return value

The all() function returns a boolean value, where true denotes that all the bits are set and false denotes that any one bit is not set.

Code

Let’s have a look at the code.

#include <iostream>
#include <bitset>
using namespace std;
int main() {
bitset<4> b1 = 15;
if (b1.all())
cout << "All bits are set in " << b1;
else
cout << "All bits are not set in " << b1;
return 0;
}

Explanation

  • In lines 1 and 2, we import the required header files.

  • In line 6, we create a bitset object that has the capacity to store 4 bits. This object will contain the binary representation of the integer 15.

  • In line 8, we call the all() function and print if all the bits are set.

  • If all the bits are not set, then, in line 11, we print the corresponding message.

In this way, we can use the bitset::all() function to check whether all the bits are set.

Free Resources