list

bitset

The bitset class supports operations on a set of bits. Its template specification is

template <size_t N> class bitset;

Here, N specifies the length of the bitset, in bits. It has the following constructors:

bitset( );
bitset(unsigned long bits);
explicit bitset(const string &s, size_t i = 0, size_t num = npos);

The first form constructs an empty bitset. The second form constructs a bitset that has its bits set according to those specified in bits. The third form constructs a bitset using the string s, beginning at i. The string must contain only 1’s and 0’s. Only num or s.size( )–i values are used, whichever is less. The constant npos is a value that is sufficiently large to describe the maximum length of s.

The I/O operators << and >> are defined for bitset.

bitset contains the following member functions:

Member

Description

bool any( ) const;

Returns true if any bit in the invoking bitset is 1. It returns false otherwise.

size_t count( ) const;

Returns the number of 1 bits.

bitset<N> &flip( );

Reverses the state of all bits in the invoking bitset and returns *this.

bitset<N> &flip(size_t i);

Reverses the bit in position i in the invoking bitset and returns *this.

bool none( ) const;

Returns true if no bits are set in the invoking bitset.

bool operator !=(const bitset<N> &op2)
  const;

Returns true if the invoking bitset differs from the one specified by right-hand operator, op2.

bool operator ==(const bitset<N> &op2)
  const;

Returns true if the invoking bitset is the same as the one specified by right-hand operator, op2.

bitset<N>
  &operator &=(const bitset<N> &op2);

ANDs each bit in the invoking bitset with the corresponding bit in op2 and leaves the result in the invoking bitset. It returns *this.

bitset<N>
  &operator ^=(const bitset<N> &op2);

XORs each bit in the invoking bitset with the corresponding bit in op2 and leaves the result in the invoking bitset. It returns *this.

bitset<N>
  &operator |=(const bitset<N> &op2);

ORs each bit in the invoking bitset with the corresponding bit in op2 and leaves the result in the invoking bitset. It returns *this.

bitset<N> &operator ~( ) const;

Reverses the state of all bits in the invoking bitset and returns the result.

bitset<N> &operator <<=(size_t num);

Left-shifts each bit in the invoking bitset num positions and leaves the result in the invoking bitset. It returns *this.

bitset<N> &operator >>=(size_t num);

Right-shifts each bit in the invoking bitset num positions and leaves the result in the invoking bitset. It returns *this.

reference operator [ ](size_t i);

Returns a reference to bit i in the invoking bitset.

bitset<N> &reset( );

Clears all bits in the invoking bitset and returns *this.

bitset<N> &reset(size_t i);

Clears the bit in position i in the invoking bitset and returns *this.

bitset<N> &set( );

Sets all bits in the invoking bitset and returns *this.

bitset<N> &set(size_t i, int val = 1);

Sets the bit in position i to the value specified by val in the invoking bitset and returns *this. Any nonzero value for val is assumed to be 1.

size_t size( ) const;

Returns the number of bits that the bitset can hold.

bool test(size_t i) const;

Returns the state of the bit in position i.

string to_string( ) const;

Returns a string that contains a representation of the bit pattern in the invoking bitset.

unsigned long to_ulong( ) const;

Converts the invoking bitset into an unsigned long integer.




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net