C++ bitset and its application
A bitset is an array of bools but each boolean value is not stored in a separate byte instead, bitset optimizes the space such that each boolean value takes 1-bit space only, so space taken by bitset is less than that of an array of bool or vector of bool.
A limitation of the bitset is that size must be known at compile time i.e. size of the bitset is fixed.
std::bitset is the class template for bitset that is defined inside <bitset> header file so we need to include the header file before using bitset in our program.
We can initialize bitset in three ways :
1. Uninitialized: All the bits will be set to zero.
2. Initialization with decimal integer: Bitset will represent the given decimal number in binary form.
3. Initialization with binary string: Bitset will represent the given binary string.
bitset<size> variable_name(string("BINARY_STRING"); bitset<size> variable_name("BINARY_STRING");
Uninitialized bitset: 00000000 Initialized with decimal: 00001111 Initialized with string: 00001111
std::bitset Member Functions
std::bitset class contains some useful member functions to work on the bitset objects. Here is the list of some member functions of std::bitset:
|Set the bit value at the given index to 1.|
|Set the bit value at a given index to 0.|
|Flip the bit value at the given index.|
|Count the number of set bits.|
|Returns the boolean value at the given index.|
|Checks if any bit is set.|
|Checks if none bit is set.|
|Check if all bit is set.|
|Returns the size of the bitset.|
|Converts bitset to std::string.|
|Converts bitset to unsigned long.|
|Converts bitset to unsigned long long.|
any() value: true all() value: true none() value: false test() at index 0: 1 size() value: 4 Value of allUnset on before using set(): 0000 Value of allUnset on after using set(): 0001 Value of allSet on before using reset(): 1111 Value of allSet on after using reset(): 1110 bitString: 1110 Unsigned Long value: 14
Note: boolalpha is used to print “true” and “false” instead of 1 or 0 for boolean values and noboolalpha for opposite.
Some of the basic operators are overloaded to work with bitset objects. Following is the list of those operators:
|Binary Right shift|
|Binary Left shift|
|Assign the value of bitwise AND to the first bitset.|
|Assign the value of bitwise OR to the first bitset.|
|Assign the value of bitwise XOR to the first bitset.|
Bitset1: 1001 Bitset2: 1010 Accessing bit value at index 1 of bitset1: 0 Bitwise AND using &: 1000 Bitwise AND using &=: 1000 Bitwise OR using &: 1011 Bitwise OR using &=: 1011 Bitwise NOT: 0100 Bitwise XOR: 0011 Binary leftshift on bitwise1: 0010 Binary rightshift on bitwise1: 0100
Difference between std::bitset and std::vector<bool> and an array of bool
Vector of bool and array of bool can also be implemented to store a sequence of boolean values like bitset but there are some differences between each implementation:
vector of bool
array of bool
|Definition||A class template consisting of a sequence of bits stored such that each bit occupies 1 bit of memory.||A variation of vectors of C++ STL in which each element is of size 1 bit and is of type bool||A fixed size contiguous collection of bool data elements.|
|Size||Fixed Size.||Dynamic Size.||Fixed Size.|
|Memory||A single element occupies 1 bit of memory.||A single element occupies 1 bit of memory.||A single element occupies 1 byte of memory.|
This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Please Login to comment...