I am a software engineer. I have been come across C++,MFC, .net technologies. I like playing video games & reading books.
1. Bit Representation Class
The c++ bitset class is useful to represent the binary number as a collection of bits or I can say a set of bits. A bitset class instance can be created using integer value or string representation of a binary number. For Example consider the below picture:
Here, the integer value 252 is represented in the binary form by utilizing 8-bit fields. The bitset class representation will look like the reverse as bit position is written starting from Left towards right. When you debug this is how the bit fields are shown as a set of fields (Refer the bit value in set braces). From the above picture, the bit position 2 (Bit value 1) in binary number representation occupies third lot counting from the right side. The bitset representation is shown as a reverse in the debugger. So do not get confused with these signs. To refer a particular bit in the bit set, we can use the operator .
2. Constructing Bitset
A Bitset can be constructed from a string form of binary numbers or it can be constructed from an integer number. To use bitmap class #include the <bitset> and this is marked as 1 in the below code snippet. The code snippet marked as two shows creating the bitset instance by making use of default constructor. In the code snippet, bitset1 instance contains 8-bit parts and each initialized to zero. You can allocate the capacity of the bitmap by specifying it in the angle bracket. For Example bitset<16> provide room to store 16 bits.
Code snippet marked as three shows constructing the object using integer number. The binary form of the number 177 is represented as eight-bit set in the instance bitset2. Code snippet marked as four shows creating the bitset object by passing the string representation of binary bits to the constructor.
Code snippet for the bitset construction given below:
3. Setting and Resetting Bits
The set and reset member function of bitset class is used to set or reset a particular bit in the bitset. The set function turns ON all the bits in the bitset. To set a particular bit we can pass the bit location as a function parameter. Simply, the set function is used to set a particular bit to 1. Similarly, the reset function call turns OFF the given bit in the bitset. Have a look at the below example:
Code snippet for setting and resetting bits are given below:
4. Testing the State of Bit
OK, we construct a bitset and set or reset a particular bit in the bitset. How do I check the status of a particular bit in the bitset object? The test function takes index location of the bit that needs to be tested. When a bit under test is in the turned-on position (i.e.) 1, then the function will return Boolean true otherwise, it returns false. The below code snippet iterates through the bit set and tests each bit status. Based on the test outcome, the bit is SET or Not SET is printed on the console output.
Note that the size() function of the bitset class returns number of bits that participates in the bitset object. Below is code snippet:
Do not get confused with count and size method. The count() method of the bitset object tells how many bits in the given bitset are in the turned-on state. The below code snippet shows that three bits are turned ON.
Below is the complete code and its output:
© 2015 sirama
Code on April 30, 2018:
email@example.com on February 05, 2018: