Cell PhonesComputersConsumer ElectronicsGraphic Design & Video EditingHome Theater & AudioIndustrial TechnologyInternet

C++ Bitset With Example

Updated on December 28, 2016
sirama profile image

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:

Bit fields represented in set form
Bit fields represented in set form | Source

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.

Constructing bitset
Constructing bitset | Source

Code snippet for the bitset construction given below:

//Constuct 01: 8 bits initialized to zero
bitset<8> bitset1;
cout << "Construction without any parameter. Content of bitset1 :" 
	<< bitset1 << endl;

//Construct 02: Construct it from a integer number
bitset<8> bitset2(177);
cout << "Constructor with int as parameter. Content of bitset2: " 
	<< bitset2 << endl;

//Construct 03: Construct it from string 
bitset<8> bitset3(string("11111100"));
cout << "Constructor with string parameter.Content of bitset3: " 
	<< bitset3 << endl;

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: Setting and resetting bitset bits
Code Snippet: Setting and resetting bitset bits | Source

Code snippet for setting and resetting bits are given below:


//Usage 01: Set and Reset a bit in the bitset
bitset2.reset(); cout << bitset2 << endl;
bitset2.set(4); cout << bitset2 << endl;
bitset2.set(7); cout << bitset2 << endl;

bitset3.set();	cout << bitset3 << endl;
bitset3.reset(1); cout << bitset3 << endl;
bitset3.reset(2); cout << bitset3 << endl;

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.

Getting status of Particular bit in the bitset instance
Getting status of Particular bit in the bitset instance | Source

Note that the size() function of the bitset class returns number of bits that participates in the bitset object. Below is code snippet:

//Usage 02: Testing a perticular Bit
cout << "Bitset3: " << bitset3 << endl;
for (int i=0; i<bitset3.size(); i++)
{
	if (bitset3.test(i))
		cout << "Bit " << i << " is Set" << endl;
	else
		cout << "Bit " << i << " is Not Set" << endl;
}

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:



/* Example 01: Bitset
Explains the usage std::bitset
*/

#include <iostream>
#include <bitset>
#include <conio.h>

using namespace std;

void main()
{
	//Constuct 01: 8 bits initialized to zero
	bitset<8> bitset1;
	cout << "Construction without any parameter. Content of bitset1 :" 
		<< bitset1 << endl;

	//Construct 02: Construct it from a integer number
	bitset<8> bitset2(177);
	cout << "Constructor with int as parameter. Content of bitset2: " 
		<< bitset2 << endl;

	//Construct 03: Construct it from string 
	bitset<8> bitset3(string("11111100"));
	cout << "Constructor with string parameter.Content of bitset3: " 
		<< bitset3 << endl;

	//Usage 01: Set and Reset a bit in the bitset
	bitset2.reset(); cout << bitset2 << endl;
	bitset2.set(4); cout << bitset2 << endl;
	bitset2.set(7); cout << bitset2 << endl;

	bitset3.set();	cout << bitset3 << endl;
	bitset3.reset(1); cout << bitset3 << endl;
	bitset3.reset(2); cout << bitset3 << endl;

	//Usage 02: Testing a perticular Bit
	cout << "Bitset3: " << bitset3 << endl;
	for (int i=0; i<bitset3.size(); i++)
	{
		if (bitset3.test(i))
			cout << "Bit " << i << " is Set" << endl;
		else
			cout << "Bit " << i << " is Not Set" << endl;
	}

	//Usage 03: Number of bits set
	bitset3.reset();
	bitset3.set(2);
	bitset3.set(4);
	bitset3.set(5);
	cout << bitset3.count() << " Bit(s) set" << endl;
	getch();
}
Program Output
Program Output | Source

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article