Namespaces
Variants
Views
Actions

std::bitset<N>::test

From cppreference.com
< cpp‎ | utility‎ | bitset
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Elementary string conversions
(C++17)
(C++17)
 
 
bool test( size_t pos ) const;

Returns the value of the bit at the position pos.

Unlike operator[], performs a bounds check and throws std::out_of_range if pos does not correspond to a valid position in the bitset.

Contents

[edit] Parameters

pos - position of the bit to return

[edit] Return value

true if the requested bit is set, false otherwise.

[edit] Exceptions

std::out_of_range if pos does not correspond to a valid position within the bitset.

[edit] Example

#include <iostream>
#include <bitset>
 
int main() 
{
    std::bitset<10> b1("1111010000");
 
    size_t idx = 0;
    while (idx < b1.size() && !b1.test(idx)) {
      ++idx;
    }
 
    if (idx < b1.size()) {
        std::cout << "first set bit at index " << idx << '\n';
    } else {
        std::cout << "no set bits\n";
    }
}

Output:

first set bit at index 4

[edit] See also

accesses specific bit
(public member function) [edit]