bit::vector — Index Locations

Find the indices of the set or unset bits in a bit-vector.

1std::vector<std::size_t> set_indices() const;
2std::vector<std::size_t> unset_indices() const;
1
Returns the index locations of the set bits in order.
2
Returns the index locations of the unset bits in order.

Example

#include <bit/bit.h>
#include <iterator>
int main()
{
1    auto v = bit::vector<>::checker_board(19);
    auto set_indices   = v.set_indices();
    auto unset_indices = v.unset_indices();

2    std::ostream_iterator<std::size_t> iter(std::cout," ");

    std::cout << "Bit-vector " << v.to_string() << " has set indices at locations:\n";
    std::copy (set_indices.begin(), set_indices.end(), iter);
    std::cout << '\n';

    std::cout << "Bit-vector " << v.to_string() << " has unset indices at locations:\n";
    std::copy (unset_indices.begin(), unset_indices.end(), iter);
    std::cout << '\n';
}
1
Creates a checker-board patterned bit-vector of size 19 and then extracts the set & unset index locations.
2
Use a stream iterator to print those indices.

Output

Bit-vector 0101010101010101010 has set indices at locations:
1 3 5 7 9 11 13 15 17
Bit-vector 0101010101010101010 has unset indices at locations:
0 2 4 6 8 10 12 14 16 18

See Also

vector::if_set_call

Back to top