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 for representing binary numbers as a collection of bits, or a set of bits. A bitset class instance can be created using an integer value or string representation of a binary number. For example, consider the picture below:
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 to right. When you debug, this is how the bit fields are shown as a set of fields (refer to the bit value in set braces). From the above picture, the bit position 2 (bit value 1) in binary number representation occupies the 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 to 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, the 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> provides room to store 16 bits.
Code snippet marked as three shows constructing the object using an 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 is 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 example below:
Code snippet for setting and resetting bits is given below:
Read More From Owlcation
4. Testing the State of Bit
Okay, 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 the number of bits that participates in the bitset object. Below is the 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:
firstname.lastname@example.org on February 05, 2018: