Namespaces
Variants
Views
Actions

std::bitset::test

From cppreference.com
< cpp‎ | utility‎ | bitset
Revision as of 12:37, 8 November 2012 by Nate (Talk | contribs)

Template:ddcl list begin <tr class="t-dcl ">

<td class="t-dcl-nopad">
bool test( size_t pos ) const;
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Returns the value of the bit at the position pos.

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

Contents

Parameters

pos - position of the bit to return

Return value

true if the requested bit is set, false otherwise.

Exceptions

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

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 << "lowest set bit at index " << idx << '\n';
    } else {
        std::cout << "no set bits\n";
    }
}

Output:

first set bit at index 4

See also

accesses specific bit
(public member function) [edit]