bitset
class template
<bitset>
A bitset is a special container class that is designed to store bits (elements with only two possible values: 0 or 1, true or false, ...).
The class is very similar to a regular array, but optimizing for space allocation: each element occupies only one bit (which is eight times less than the smallest elemental type in C++: char).
Each element (each bit) can be accessed individually: for example, for a given bitset named mybitset, the expression mybitset[3] accesses its fourth bit, just like a regular array accesses its elements.
Because no such small elemental type exists in most C++ environments, the individual elements are accessed as special references which mimic bool elements:
Apart from overriding several operators and to provide direct access to the bits, bitsets have the feature of being able to be constructed from and converted to both integer values and binary strings (see constructor, bitset::to_ulong and bitset::to_string). They can also be directly inserted and extracted from streams in binary format.
Bitsets have a fixed size. For a similar container class that also optimizes for space allocation and allows for dynamic resizing, see the bool specialization of vector(vector<bool>).
In their implementation in the C++ Standard Template Library, bitsets take a single template parameter:
Where the template parameter has the following meaning:
Bit access:
Bit operations:
Bitset operations:
The class is very similar to a regular array, but optimizing for space allocation: each element occupies only one bit (which is eight times less than the smallest elemental type in C++: char).
Each element (each bit) can be accessed individually: for example, for a given bitset named mybitset, the expression mybitset[3] accesses its fourth bit, just like a regular array accesses its elements.
Because no such small elemental type exists in most C++ environments, the individual elements are accessed as special references which mimic bool elements:
1 2 3 4 5 6 7 8 9 10 11 |
|
Apart from overriding several operators and to provide direct access to the bits, bitsets have the feature of being able to be constructed from and converted to both integer values and binary strings (see constructor, bitset::to_ulong and bitset::to_string). They can also be directly inserted and extracted from streams in binary format.
Bitsets have a fixed size. For a similar container class that also optimizes for space allocation and allows for dynamic resizing, see the bool specialization of vector(vector<bool>).
In their implementation in the C++ Standard Template Library, bitsets take a single template parameter:
|
Where the template parameter has the following meaning:
- N: Number of bits to contain (size_t is an integral type).
Member functions
(constructor) | Construct bitset (public member function) |
applicable operators | Bitset operators (functions) |
Bit access:
operator[] | Access bit (public member function) |
Bit operations:
set | Set bits (public member function) |
reset | Reset bits (public member function) |
flip | Flip bits (public member function) |
Bitset operations:
to_ulong | Convert to unsigned long integer (public member function) |
to_string | Convert to string (public member function) |
count | Count bits set (public member function) |
size | Return size (public member function) |
test | Return bit value (public member function) |
any | Test if any bit is set (public member function) |
none | Test if no bit is set (public member function) |
bitset::bitset
public member function
bitset ( ); bitset ( unsigned long val ); template<class charT, class traits, class Allocator> explicit bitset ( const basic_string<charT,traits,Allocator>& str, typename basic_string<charT,traits,Allocator>::size_type pos = 0, typename basic_string<charT,traits,Allocator>::size_type n = basic_string<charT,traits,Allocator>::npos);
Construct bitset
Constructs a bitset container object.
If the default constructor is used, the bitset is initialized with zeros, otherwise its initial content depends on the parameters used:
Bitsets have a fixed size, which is determined by their class's template parameter:
Where N is the size, specified as an integer value of type size_t.
The code does not produce any output, but demonstrates some ways in which a bitset container can be constructed.
If the default constructor is used, the bitset is initialized with zeros, otherwise its initial content depends on the parameters used:
Bitsets have a fixed size, which is determined by their class's template parameter:
|
Where N is the size, specified as an integer value of type size_t.
Parameters
- val
- Integral value whose bits are copied to the bitset elements, up to the smaller of the bitset size and the size in bits of an unsigned long.
- str
- String containing a binary value (zeros and ones). These are parsed and used to initizialize the bits. If some character other than zero or one is found, the function throws an invalid_argument exception.
- pos
- First character in the string to be read as the content for the bitset.
- n
- Number of characters to read. A value of string::npos specifies that as many characters as possible are to be read.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
The code does not produce any output, but demonstrates some ways in which a bitset container can be constructed.
bitset operators
functions
** bitset member functions: *** bitset<N>& operator&= (const bitset<N>& rhs); bitset<N>& operator|= (const bitset<N>& rhs); bitset<N>& operator^= (const bitset<N>& rhs); bitset<N>& operator<<= (const bitset<N>& rhs); bitset<N>& operator>>= (const bitset<N>& rhs); bitset<N> operator~() const; bitset<N> operator<<(size_t pos) const; bitset<N> operator>>(size_t pos) const; bool operator== (const bitset<N>& rhs) const; bool operator!= (const bitset<N>& rhs) const; *** global functions: *** template<size_t N> bitset<N> operator& (const bitset<N>& lhs, const bitset<N>& rhs); template<size_t N> bitset<N> operator| (const bitset<N>& lhs, const bitset<N>& rhs); template<size_t N> bitset<N> operator^ (const bitset<N>& lhs, const bitset<N>& rhs); *** iostream global functions (extraction/insertion): *** template<class charT, class traits, size_t N> basic_istream<charT, traits>& operator>> (basic_istream<charT,traits>& is, bitset<N>& rhs); template<class charT, class traits, size_t N> basic_ostream<charT, traits>& operator<< (basic_ostream<charT,traits>& os, bitset<N>& rhs);
Bitset containers support a different set of operators than the other container classes, mainly to support bitwise operators and to allow them to be insert/extracted directly to/from streams.
All these operators emulate for the bitset container the bitwise logic which applies to fundamental data types.
Global functions: The left-hand side object (lhs, is or os).
All these operators emulate for the bitset container the bitwise logic which applies to fundamental data types.
Parameters
- lhs
- Left-hand side bitset object (global functions). It must be of the same amount of bits as the right-hand side bitset object (i.e. with the same N template parameter).
- rhs
- Right-hand side bitset object. For member functions, rhs must have the same amount of bits as the bitset object.
- is,os
- istream or ostream object from which a bitset object is respectively extracted or inserted. The format in which bitsets are inserted/extracted is binary (successions of 0's and 1's).
Return value
Member functions: either *this or the result of the comparison.Global functions: The left-hand side object (lhs, is or os).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
bitset::bitset
public member function
bitset ( ); bitset ( unsigned long val ); template<class charT, class traits, class Allocator> explicit bitset ( const basic_string<charT,traits,Allocator>& str, typename basic_string<charT,traits,Allocator>::size_type pos = 0, typename basic_string<charT,traits,Allocator>::size_type n = basic_string<charT,traits,Allocator>::npos);
Constructs a bitset container object.
If the default constructor is used, the bitset is initialized with zeros, otherwise its initial content depends on the parameters used:
Bitsets have a fixed size, which is determined by their class's template parameter:
Where N is the size, specified as an integer value of type size_t.
The code does not produce any output, but demonstrates some ways in which a bitset container can be constructed.
If the default constructor is used, the bitset is initialized with zeros, otherwise its initial content depends on the parameters used:
Bitsets have a fixed size, which is determined by their class's template parameter:
|
Where N is the size, specified as an integer value of type size_t.
Parameters
- val
- Integral value whose bits are copied to the bitset elements, up to the smaller of the bitset size and the size in bits of an unsigned long.
- str
- String containing a binary value (zeros and ones). These are parsed and used to initizialize the bits. If some character other than zero or one is found, the function throws an invalid_argument exception.
- pos
- First character in the string to be read as the content for the bitset.
- n
- Number of characters to read. A value of string::npos specifies that as many characters as possible are to be read.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
The code does not produce any output, but demonstrates some ways in which a bitset container can be constructed.
bitset::set
public member function
bitset<N>& set ( ); bitset<N>& set ( size_t pos, bool val = true );
The version with no parameters sets (to 1) all the bits in the bitset.
The parameterized version, stores val as the new value for bit at position pos.
If pos is not a valid bit position, out_of_range is thrown.
public member functionReset bits
public member functionFlip bits
The parameterized version, stores val as the new value for bit at position pos.
Parameters
- pos
- Order position of the bit whose value is modified. Order positions are counted from the rightmost bit, which is order position 0. size_t is an unsigned integral type.
- val
- Value to store in the bit (either true or false).
Return value
*thisIf pos is not a valid bit position, out_of_range is thrown.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
bitset::reset
public member function
bitset<N>& reset ( ); bitset<N>& reset ( size_t pos );
The version with no parameters resets all the bits in the bitset (sets al bits to 0).
The parameterized version, resets (sets to 0) the bit at position pos.
If pos is not a valid bit position, out_of_range is thrown.
The parameterized version, resets (sets to 0) the bit at position pos.
Parameters
- pos
- Order position of the bit whose value is modified. Order positions are counted from the rightmost bit, which is order position 0. size_t is an unsigned integral type.
Return value
*thisIf pos is not a valid bit position, out_of_range is thrown.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
bitset::flip
public member function
bitset<N>& flip ( ); bitset<N>& flip ( size_t pos );
The version with no parameters flips all the bits in the bitset, i.e. changes all 0s for 1s and all 1s for 0s.
The parameterized version, flips only the bit at position pos.
The unary operator~ performs the same operation as flip().
If pos is not a valid bit position, out_of_range is thrown.
public member functionAccess bit
The parameterized version, flips only the bit at position pos.
The unary operator~ performs the same operation as flip().
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
*thisIf pos is not a valid bit position, out_of_range is thrown.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
bitset::operator[]
public member function
bool operator[] ( size_t pos ) const; reference operator[] ( size_t pos );
The function returns the value (or a reference) to the bit at position pos.
With this operator, no range check is performed. Use bitset::test to read the value with bitset bounds checked.
functionsBitset operators
With this operator, no range check is performed. Use bitset::test to read the value with bitset bounds checked.
Parameters
- pos
- Order position of the bit whose value is accessed. Order positions are counted from the rightmost bit, which is order position 0. size_t is an unsigned integral type.
Return value
*thisExample
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
bitset operators
functions
** bitset member functions: *** bitset<N>& operator&= (const bitset<N>& rhs); bitset<N>& operator|= (const bitset<N>& rhs); bitset<N>& operator^= (const bitset<N>& rhs); bitset<N>& operator<<= (const bitset<N>& rhs); bitset<N>& operator>>= (const bitset<N>& rhs); bitset<N> operator~() const; bitset<N> operator<<(size_t pos) const; bitset<N> operator>>(size_t pos) const; bool operator== (const bitset<N>& rhs) const; bool operator!= (const bitset<N>& rhs) const; *** global functions: *** template<size_t N> bitset<N> operator& (const bitset<N>& lhs, const bitset<N>& rhs); template<size_t N> bitset<N> operator| (const bitset<N>& lhs, const bitset<N>& rhs); template<size_t N> bitset<N> operator^ (const bitset<N>& lhs, const bitset<N>& rhs); *** iostream global functions (extraction/insertion): *** template<class charT, class traits, size_t N> basic_istream<charT, traits>& operator>> (basic_istream<charT,traits>& is, bitset<N>& rhs); template<class charT, class traits, size_t N> basic_ostream<charT, traits>& operator<< (basic_ostream<charT,traits>& os, bitset<N>& rhs);
Bitset containers support a different set of operators than the other container classes, mainly to support bitwise operators and to allow them to be insert/extracted directly to/from streams.
All these operators emulate for the bitset container the bitwise logic which applies to fundamental data types.
Global functions: The left-hand side object (lhs, is or os).
public member functionConvert to unsigned long integer
All these operators emulate for the bitset container the bitwise logic which applies to fundamental data types.
Parameters
- lhs
- Left-hand side bitset object (global functions). It must be of the same amount of bits as the right-hand side bitset object (i.e. with the same N template parameter).
- rhs
- Right-hand side bitset object. For member functions, rhs must have the same amount of bits as the bitset object.
- is,os
- istream or ostream object from which a bitset object is respectively extracted or inserted. The format in which bitsets are inserted/extracted is binary (successions of 0's and 1's).
Return value
Member functions: either *this or the result of the comparison.Global functions: The left-hand side object (lhs, is or os).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
bitset::to_ulong
public member function
unsigned long to_ulong ( ) const;
Returns an unsigned long with the integer value that has the same bits set as the bitset.
If the bitset is too long to be represented as an unsigned long, an overflow_error exception is thrown.
Output:
public member functionConvert to string
public member functionCount bits set
public member functionReturn size
Parameters
noneReturn value
Integer value with the same bits set as the bitset.If the bitset is too long to be represented as an unsigned long, an overflow_error exception is thrown.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Output:
1111 as an integer is: 15 |
bitset::to_string
public member function
template <class charT, class traits, class Allocator> basic_string<charT,traits,Allocator> to_string() const;
Constructs a basic_string object that represents the bitset as a succession of zeros and ones.
Notice that this function template uses the template parameters to define the return type. Therefore, they are not implicitly deduced by the compiler.
Output:
Notice that this function template uses the template parameters to define the return type. Therefore, they are not implicitly deduced by the compiler.
Parameters
noneReturn value
String representing the bitset.Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Output:
1111 |
bitset::count
public member function
size_t count ( );
Returns the amount of bits in the bitset that are set (i.e., have a value of 1).
size_t is an unsigned integral type.
Output:
Parameters
noneReturn value
The number of bits set.size_t is an unsigned integral type.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Output:
myset has 5 ones, and 3 zeros. |
bitset::size
public member function
size_t size() const;
Returns the number of bits in the bitset.
size_t is an unsigned integral type.
Output:
public member functionTest if any bit is set
public member functionTest if no bit is set
public member functionReturn bit value
Parameters
noneReturn Value
The number of bits in the bitset. This is the template parameter N.size_t is an unsigned integral type.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Output:
first.size() is 8 second.size() is 4 |
bitset::any
public member function
bool any ( ) const;
Returns whether any of the bits in the bitset is set.
Parameters
noneReturn value
true if any of the bits in the bitset is set, and false otherwise.Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
bitset::none
public member function
bool none ( ) const;
Returns whether none of the bits in the bitset are set.
Parameters
noneReturn value
true if none of the bits in the bitset are set, and false otherwise.Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
bitset::test
public member function
bool test ( size_t pos ) const;
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.
If pos is not a valid bit position, out_of_range is thrown.
Output:
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Output:
mybits contains: true true false true false |
Không có nhận xét nào:
Đăng nhận xét