GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::bitset::test(3) C++ Standard Libary std::bitset::test(3)

std::bitset::test - std::bitset::test


bool test( std::size_t pos ) const; (until C++23)
constexpr bool test( std::size_t pos ) const; (since C++23)


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.


pos - position of the bit to return


true if the requested bit is set, false otherwise.


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

// Run this code


#include <iostream>
#include <bitset>
#include <bit>
#include <cassert>
#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 << "first set bit at index " << idx << '\n';
} else {
std::cout << "no set bits\n";
}


try {
if (b1.test(b1.size()))
std::cout << "Expect unexpected!\n";
} catch (std::out_of_range const& ex) {
std::cout << "Exception: " << ex.what() << '\n';
}
}


first set bit at index 4
Exception: bitset::test: __position (which is 10) >= _Nb (which is 10)


operator[] accesses specific bit
(public member function)
popcount counts the number of 1 bits in an unsigned integer
(C++20) (function template)
has_single_bit checks if a number is an integral power of two
(C++20) (function template)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.