C++ Standard Library C++ STL Library

C++ <bitset> - any() Function



The C++ bitset::any function is used to check if any bit of the bitset is set (i.e., check whether at least one bit is set to 1).

Syntax

bool any() const;
bool any() const noexcept;

Parameters

No parameter is required.

Return Value

true if any bit is set to 1, false otherwise.

Exception

Never throws exceptions.

Example:

In the example below, the bitset::any function is used to check whether any bit in the given bitset is set or not.

#include <iostream>
#include <bitset>
using namespace std;
 
int main (){
  bitset<5> bset;

  if(!bset.any()) 
    cout<<"All bits in bset are unset.\n";

  bset =  bitset<5>("11011");

  if(bset.any()) 
    cout<<"At least one bit in bset is set.\n";

  return 0;
}

The output of the above code will be:

All bits in bset are unset.
At least one bit in bset is set.

❮ C++ <bitset> Library