C++

C++ bitset Function with Examples

When it comes to programming, bit manipulation is an essential concept that everyone must understand. One of the useful functions in C++ is the bitset function, which is used to manipulate binary data. This article covers the bitset function in C++ and how it can be used to manipulate bits.

Table of Contents

1. Introduction to the bitset Function

2. Syntax of the bitset Function

3. Creating a bitset Object

4. Accessing and Manipulating Bit Values

5. Bitwise Operations on bitset

6. Conversion to Other Data Types

7. Conclusion

1. Introduction to the bitset Function

The bitset function is a C++ Standard Library function that is used to store a sequence of binary digits, commonly referred to as bits. The bitset function provides a convenient way to manipulate and perform bitwise operations on binary data.

The bitset function is implemented as a class template that takes an integer value as a template parameter, which specifies the size of the bitset.

2. Syntax of the bitset Function

The syntax of the bitset function is straightforward. It is defined as follows:

#include<bitset>

bitset<N> b;

Here, N is the number of bits that the bitset can hold, and b is the name of the bitset object. The <bitset> header file must be included to use the bitset function.

3. Creating a bitset Object

To create a bitset object, we must first declare it, then initialize it using one of the following methods:

3.1. Default Initialization

The bitset object can be initialized to all zeros by default, as shown below:

#include<iostream>

#include<bitset>

using namespace std;

int main() {

bitset<8> b; //creates a bitset of size 8 with all bits set to 0

  //Output: 00000000

cout << b << endl;

  return 0;

}

This code first includes the necessary headers iostream and bitset. It then creates a bitset object b of size 8 with all bits set to 0. Finally, it outputs the value of b to the screen using the cout statement. The output should be 00000000.

3.2. Initialization with an Integer Value

The bitset object can also be initialized from an integer value, as shown below:

#include <iostream>

#include <bitset>

using namespace std;

int main() {

bitset<8> b(255); //creates a bitset of size 8 initialized to 11111111

//Output: 11111111

cout << b << endl;

  return 0;

}

In the above-given code, the bitset<8> statement creates a bitset object of size 8. The constructor is initialized with the decimal value 255, which is equivalent to the binary value of 11111111.

The cout << b << endl; statement outputs the bitset object b to the console. This will output the binary value of b, which is 11111111 in this case.

3.3. Initialization with Binary Values

The bitset object can also be initialized from a binary string, as shown below:

#include <iostream>

#include <bitset>

using namespace std;

int main() {

bitset<8> b("10101010"); //creates a bitset of size 8 initialized to 10101010

  //Output: 10101010

cout << b << endl;

  return 0;

}

This code first includes the necessary headers iostream and bitset. It then creates a bitset object b of size 8 initialized to 10101010. Finally, it outputs the value of b to the screen using the cout statement. The output should be 10101010.

4. Accessing and Manipulating Bit Values

The bitset function provides methods to access and manipulate individual bits in a bitset object.

4.1. Accessing Bit Values

The operator [ ] method is used to access individual bits in a bitset object. It takes an index value as a parameter and output bit value at that specific index:

#include <iostream>

#include <bitset>

using namespace std;

int main() {

bitset<8> b("10101010"); //creates a bitset of size 8 initialized to 10101010

  //Output: 0

cout << b[0] << endl;

  //Output: 1

cout << b[1] << endl;

  return 0;

}

This code first includes the necessary headers iostream and bitset. It then creates a bitset object b of size 8 initialized to 10101010. Finally, it accesses the values of the individual bits at positions 0 and 1 using the [ ] operator and outputs them to the screen using the cout statement. The output should be 0 and 1 respectively.

4.2. Manipulating Bit Values

The set, reset, and flip methods are used to manipulate individual bits in a bitset object.

The set method is used to set the bit value at a given index to 1.

#include <iostream>

#include <bitset>

using namespace std;

int main() {

bitset<8> b("00000000"); //creates a bitset of size 8 initialized to 00000000

b.set(2); //sets the value of bit at index 2 to 1

  //Output: 00000100

cout << b << endl;

  return 0;

}

This code first includes the necessary headers iostream and bitset. It then creates a bitset object b of size 8 initialized to 00000000. Next, it uses the set method to set the value of the bit at index 2 to 1. Finally, it outputs the updated value of the bitset to the console using the cout statement. The output should be 00000100.

The reset method is used to set the bit value to 0 at a specific index.

#include <iostream>

#include <bitset>

using namespace std;

int main() {

bitset<8> b("11111111"); //creates a bitset of size 8 initialized to 11111111

b.reset(2); //sets the value of bit at index 2 to 0

  //Output: 11111011

cout << b << endl;

  return 0;

}

This code first includes the necessary headers iostream and bitset. It then creates a bitset object b of size 8 initialized to 11111111. Next, it uses the reset method to set the value of the bit at index 2 to 0. Finally, it outputs the updated value of the bitset to the screen using the cout statement. The output should be 11111011.

Graphical user interface Description automatically generated with medium confidence

The flip method is used to invert the value of a bit at a given index.

#include <iostream>

#include <bitset>

using namespace std;

int main() {

bitset<8> b("10101010"); //creates a bitset of size 8 initialized to 10101010

b.flip(2); //inverts the value of bit at index 2

  //Output: 10101110

cout << b << endl;

  return 0;

}

This code first includes the necessary headers iostream and bitset. It then creates a bitset object b of size 8 initialized to 10101010. Next, it uses the flip method to invert the value of the bit at index 2. Finally, it outputs the updated value of the bitset to the screen using the cout statement. The output should be 10101110.

Graphical user interface, text Description automatically generated

5. Bitwise Operations on bitset

The bitset function provides bitwise operators to perform bitwise operations on bitset objects.

5.1. AND Operation

The & operator performs an AND operation between two bitset objects.

#include <iostream>

#include <bitset>

using namespace std;

int main() {

bitset<8> b1("10101010"); //creates a bitset of size 8 initialized to 10101010

bitset<8> b2("11001100"); //creates a bitset of size 8 initialized to 11001100

  //Output: 10001000

cout << (b1 & b2) << endl;

  return 0;

}

This code includes the necessary header files (iostream and bitset) and declares a main() function. It also initializes two bitset objects (b1 and b2) with the given binary strings.

The & operator performs a bitwise AND operation on b1 and b2, and the result is printed to the console using the cout:

Text Description automatically generated with medium confidence

5.2. OR Operation

The pipe | operator performs an OR operation between two bitset objects.

#include <iostream>

#include <bitset>

using namespace std;

int main() {

bitset<8> b1("10101010"); //creates a bitset of size 8 initialized to 10101010

bitset<8> b2("11001100"); //creates a bitset of size 8 initialized to 11001100

  //Output: 11101110

cout << (b1 | b2) << endl;

  return 0;

}

Code started by including necessary header files (iostream and bitset) and declares a main() function. It initializes b1 and b2 with the given binary strings and performs a bitwise OR operation between them using the | operator. The result is displayed on the console using the cout statement.

5.3. XOR Operation

The caret ^ operator performs an XOR operation between two bitset objects.

#include <iostream>

#include <bitset>

using namespace std;

int main() {

bitset<8> b1("10101010"); //creates a bitset of size 8 initialized to 10101010

bitset<8> b2("11001100"); //creates a bitset of size 8 initialized to 11001100

  //Output: 01100110

cout << (b1 ^ b2) << endl;

  return 0;

}

Code begins with header files (iostream and bitset) and declares a main() function. It initializes b1 and b2 with the given binary strings and performs a bitwise XOR operation between them using the ^ operator. Output is printed using cout.

A picture containing text Description automatically generated

5.4. NOT Operation

The tilde ~ operator performs a NOT operation on a bitset object.

#include <iostream>

#include <bitset>

using namespace std;

int main() {

bitset<8> b("10101010"); //creates a bitset of size 8 initialized to 10101010

  //Output: 01010101

cout << (~b) << endl;

  return 0;

}

This code started by including header files (iostream and bitset) and declaring a main() function. It initializes b with the given binary string and performs a bitwise NOT operation on it using the ~ operator. The cout statement will display output on the console.

A screenshot of a computer Description automatically generated with medium confidence

6. Conversion to Other Data Types

The bitset function provides methods to convert a bitset object to other data types.

6.1. To Integer

The to_ulong method is used to convert a bitset object to an unsigned long integer.

#include <iostream>

#include <bitset>

using namespace std;

int main() {

bitset<8> b("10101010"); //creates a bitset of size 8 initialized to 10101010

  //Output: 170

cout << b.to_ulong() << endl;

  return 0;

}

At the start of the code, we included the necessary header files (iostream and bitset) and declare a main() function. It initializes b with the given binary string and converts it to its equivalent unsigned long value using the to_ulong() function. The cout statement will print the result.

Text Description automatically generated with medium confidence

6.2. To String

The to_string method is used to convert a bitset object to a string.

#include <iostream>

#include <bitset>

using namespace std;

int main() {

bitset<8> b("10101010"); //creates a bitset of size 8 initialized to 10101010

  //Output: 10101010

cout << b.to_string() << endl;

  return 0;

}

This code also includes the necessary header files (iostream and bitset) and declares a main() function. It initializes b with the given binary string and converts it to its equivalent binary string representation using the to_string() function. By using the cout statement, the result is output to the console.

7. Conclusion

In this article, we have discussed the bitset function in C++. We have seen how to create a bitset object, how to access and manipulate individual bits in a bitset object, how to perform bitwise operations on bitset objects, and how to convert a bitset object to other data types. By using the bitset function, we can easily create and manipulate bits in our C++ programs.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.