In the C++ programming language, a bitset is one of the elemental containers of the Standard Template Library (STL) that holds a fixed and defined length of bits, either in 0s and 1s. It is mainly defined in the <bitset> header file of C++. The bitset any() function is a member function of the bitset class, which is a part of the STL.

In C++, the bitset any() function is commonly utilized to check if at least one bit in the bitset is set to 1. It returns a true value if any of the bits in the bitset are 1's, and returns a false value if all of the bits are not non-zero. It should be used with the determination of whether or not any of the flags, status indicators, or feature indicators are set and whether or not a condition or flag is active.
It has the following syntax:
In this syntax,
Here, we are going to discuss several examples of bitset any() function.
This example demonstrates how to check whether at least one bit is set in a bitset using the any() function.
Output:
bits1: 00000000 bits2: 10100010 bits1.any(): 0 bits2.any(): 1
Explanation:
In this example, we have taken two bitsets, called bits1 and bits2. After that, we use the any() function that determines whether there are any bits of value one. It means that bits1 returns a false (0) value for all its bits that are of value 0. However, the bits2 returns a true (1) value because some of its bits are 1.
This example demonstrates how to update bits in a bitset and verify them using the any() function.
Output:
Initial bitset: 00000 Any bit set? No Updated bitset: 10100 Any bit set now? Yes
Explanation:
In this example, we create a bitset with a size of 5 bits, with all bits equal to 0. First, we use the any() function to check if any bit is set, which returns a false value because all bits are set to 0. After that, the bits of the second and fourth are set to 1 using the set() function. After updating the bitset with 1, it returns a true value because the bitset now contains at least one set bit.
This example demonstrates how to use the any() function inside conditional statements to check active bits in a bitset.
Output:
At least one bit is set in the bitset!
Explanation:
In this example, we have taken a bitset of size 5 that is initialized with 00101. After that, we use the any() function in a condition statement to check whether at least one bit is set to 1. After checking the bits in the bitset, it returns a true value because it contains at least one bit that is set to 1.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India