C++ bitset any() Function

Last Updated : 13 May 2026

Bitset any() Function

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.

C++ bitset any() Function

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.

Syntax

It has the following syntax:

In this syntax,

  • It doesn't have any parameters.

Return Value

  • True: It returns a true value if one or more bits are set in the bitset.
  • False: It returns a false value if all bits are 0.

Example of bitset any() Function

Here, we are going to discuss several examples of bitset any() function.

Example 1: Check Whether Any Bit is Set Using any()

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.

Example 2: Check and Update Bits Using any()

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.

Example 3: Use any() Function with Conditional Statements

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.


Next TopicC++ bitset