Overview
The standard library provides bitset to compactly store numbers as bits, access individual bit and perform logical operations on it.
Details
bitset is a template based compile time data structures that compactly stores numbers as 0 and 1 bits. Individual bits can be accessed by indexers that can be set or reset and logical operations such as and, or. xor etc can be performed.
Syntax
The syntax is as below. Template parameter N represents number of bits.
template < size_t N >class bitset;
Members
It defines following types
Name | Description |
---|---|
reference | proxy class to access individual bits |
Operation
bitset can be graphically represented as below.
It's a template based, compile instantiation. Individual bits can be accessed using a subclass called reference which is returned by the indexers Using this, individual bits can be flipped.
class bitset::reference{ friend class bitset; reference() noexcept; // no public constructor public: ~reference(); operator bool() const noexcept; // convert to bool reference& operator= (bool x) noexcept; // assign bool reference& operator= (const reference& x) noexcept; // assign bit reference& flip() noexcept; // flip bit value bool operator~() const noexcept; // return inverse value }
Complexity
The complexity of access operation is O(1). Insertion or removal is O(N).
Functionality
Constructors
Name | Description |
---|---|
bitset<size_t N>() | Default Constructor. Example: //{0,0,0,0,0} bitset<5> v; |
bitset<size_t N> (unsigned long long n) | Constructs bitset from a unsigned long long. Example: //{1,0,1,0,1} bitset<5> v(21); |
bitset<size_t N> (const string& str, size_type n = npos, char zero = char('0'), char one = char('1')) | Constructs a bitset from a string or its substring. Example: //{1,0,1,0,1} bitset<5> b(string("10101")); //{1,0,1,0,0} bitset<5> b2(string("10101"),2,3); //{1,0,0,0,0} bitset<5> b3(string("10101"),3,2); |
bitset<size_t N> (const char* str, size_type pos = 0, size_type n = -1, char zero = char('0'), char one = char('1')) | Constructs a bitset from a cstring or its substring. Example: //{1,0,1,0,1} bitset<5> b("10101"); //{1,0,1,0,0} bitset<5> b2("10101",2,3); //{1,0,0,0,0} bitset<5> b3("10101",3,2); |
bit access and info
Name | Description |
---|---|
|
|
| |
size_t count() | Returns number of bits that are 1. Example:
|
size_t size() | Returns total number of bits. |
bool test (size_t pos) | Returns true if the bit at pos is set, otherwise false. Exception is thrown for invalid input.Example: |
bool any() | Returns true if any of the bit is set(1), otherwise false. Example: |
bool all() | Returns true if any of the bit is set(1), otherwise false.Example: |
bool none() | Returns true if none of the bits is set(1), otherwise false.Example: |
Modifiers
Name | Description |
---|---|
|
|
| |
|
|
| |
|
|
| |
ulong to_ulong() | Returns the ulong representation of the bitset. Throws Exceptions.Example: //b:{1,0,1,0,1} bitset<5> b("10101"); //prints 21 cout << b.to_ulong() << endl; bitset<100> b2;//b2:11111111111111111111111111111111111111 |
string to_string() | Returns the string representation of the bitset. Example: //b:{1,0,1,0,1} bitset<5> b("10101"); //prints "10101" cout << b.to_string() << endl; |
No comments:
Post a Comment