Description:
std::bitset
is a template class in C++ that allows the creation of an array of bits. It supports bitwise operations and is useful for low-level bit manipulation tasks.Syntax:
where N
is the number of bits.
std::bitset<N> b;
Example:
#include <iostream>
#include <bitset>
int main() {
std::bitset<8> b1(42); // 42 in binary is 00101010
std::cout << "Bitset b1: " << b1 << std::endl; // Output: 00101010
// Bitwise operations
std::bitset<8> b2(15); // 00001111
std::cout << "Bitset b2: " << b2 << std::endl; // Output: 00001111
std::cout << "b1 & b2: " << (b1 & b2) << std::endl; // AND Operation
std::cout << "b1 | b2: " << (b1 | b2) << std::endl; // OR Operation
std::cout << "b1 ^ b2: " << (b1 ^ b2) << std::endl; // XOR Operation
std::cout << "b1 << 1: " << (b1 << 1) << std::endl; // Left Shift
std::cout << "b1 >> 1: " << (b1 >> 1) << std::endl; // Right Shift
return 0;
}