Welcome to our comprehensive guide on C++ bitset! In this lesson, we'll explore the power of bitset and learn how to leverage it for efficient coding. By the end of this tutorial, you'll be well-equipped to apply bitset in your own projects. š
In C++, bitset is a standard template library that allows us to work with bits (binary digits) directly. It simplifies binary operations, making it easier to manipulate individual bits in a more efficient way.
Before diving into bitset, let's briefly touch upon binary numbers and bitwise operations. A binary number is a number represented in base-2, consisting of only zeros and ones.
Bitwise operations are mathematical operations performed on individual bits. They include operations like AND (&), OR (|), XOR (^), NOT (~), left shift (<<) and right shift (>>).
Now that we understand binary numbers and bitwise operations, let's dive into the bitset class.
The bitset class takes a template argument n, which specifies the number of bits it will manage.
#include <bitset>
bitset<10> my_bitset; // Creates a bitset with 10 bitsYou can initialize a bitset in several ways:
bitset<10> my_bitset; // All bits are initially set to zerobitset<10> my_bitset(1234); // Initializes the bitset with the binary representation of 1234bitset<10> my_bitset(1, 0, 1, 0, 1, 0, 1, 0, 1, 0); // Manually sets each bitTo access a specific bit, use the square bracket operator. Remember, indexing starts from 0:
my_bitset[2] = 1; // Sets the 3rd bit to 1To check if a specific bit is set (i.e., it equals 1):
if (my_bitset[2]) {
// The 3rd bit is set
}You can perform all bitwise operations (AND, OR, XOR, NOT, left shift, right shift) with the bitset class.
// Example of performing bitwise operations
bitset<10> bitset1(1110101010);
bitset<10> bitset2(0101010101);
// AND operation
bitset<10> result_and = bitset1 & bitset2;
// OR operation
bitset<10> result_or = bitset1 | bitset2;
// XOR operation
bitset<10> result_xor = bitset1 ^ bitset2;
// Left shift operation
bitset<10> result_left_shift = bitset1 << 2;
// Right shift operation
bitset<10> result_right_shift = bitset1 >> 2;Now let's look at some practical examples.
Example 1: Checking if a number is odd or even
#include <iostream>
#include <bitset>
int main() {
int number = 5;
bitset<32> bits(number);
if (bits[0]) {
std::cout << number << " is odd" << std::endl;
} else {
std::cout << number << " is even" << std::endl;
}
return 0;
}Example 2: Counting the number of set bits
#include <iostream>
#include <bitset>
int count_set_bits(unsigned int num) {
unsigned int count = 0;
for (int i = 0; i < 32; ++i) {
if ((num & (1 << i))) {
++count;
}
}
return count;
}
int main() {
unsigned int number = 11;
std::cout << "Number of set bits in " << number << " is: " << count_set_bits(number) << std::endl;
return 0;
}Congratulations! You've learned the basics of using bitset in C++. By mastering bitset, you can simplify your code, improve efficiency, and tackle complex problems with ease.
What is the output of the following code?