std::bitset<N>::test

Returns the value of the bit at the position pos (counting from 0).

# Declarations

bool test( std::size_t pos ) const;

(constexpr since C++23)

# Parameters

# Return value

true if the requested bit is set, false otherwise.

# Example

#include <bit>
#include <bitset>
#include <cassert>
#include <iostream>
#include <stdexcept>
 
int main()
{
    std::bitset<10> b1("1111010000");
 
    std::size_t idx = 0;
    while (idx < b1.size() && !b1.test(idx))
        ++idx;
 
    assert(static_cast<int>(idx) == std::countr_zero(b1.to_ulong()));
 
    if (idx < b1.size())
        std::cout << "The first set bit is at index " << idx << '\n';
    else
        std::cout << "no set bits\n";
 
    try
    {
        std::bitset<0B10'1001'1010> bad;
        if (bad.test(bad.size()))
            std::cout << "Expect unexpected!\n";
    }
    catch (std::out_of_range const& ex)
    {
        std::cout << "Exception: " << ex.what() << '\n';
    }
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2250C++98the behavior was undefined if pos doesnot correspond to a valid bit positionalways throws anexception in this case

# See also