cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : bitset : test
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
bitset
bitset::bitset
bitset operators
member functions:
· bitset::any
· bitset::count
· bitset::flip
· bitset::none
· bitset::operator[]
· bitset::reset
· bitset::set
· bitset::size
· bitset::test
· bitset::to_string
· bitset::to_ulong

-

bitset::test public member function
bool test ( size_t pos ) const;

Return bit value

Returns whether bit at position pos in the bitset is set.

Unlike access operator ([]), this function perform a range check on pos before retrieveing the bit value.

Parameters

pos
Order position of the bit whose value is flipped.
Order positions are counted from the rightmost bit, which is order position 0.
size_t is an unsigned integral type.

Return value

true if bit at position pos is set, and false otherwise.

If pos is not a valid bit position, out_of_range is thrown.

Example

// bitset::test
#include <iostream>
#include <string>
#include <bitset>
using namespace std;

int main ()
{
  bitset<5> mybits (string("01011"));

  cout << "mybits contains:\n";
  cout << boolalpha;
  for (size_t i=0; i<mybits.size(); ++i)
    cout << mybits.test(i) << endl;

  return 0;
}

Output:

mybits contains:
true
true
false
true
false

See also

bitset::operator[] Access bit (public member function)
bitset::count Count bits set (public member function)
bitset::any Test if any bit is set (public member function)
bitset::none Test if no bit is set (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us