During my time working on Bitmask Class, I focused on demonstrating the use of bitmask operations in C++. Here are some of the key achievements and contributions:
Below are some key code snippets demonstrating various bitmask operations:
#include <iostream>
class Bitmask {
public:
// Set a bit
void setBit(int &num, int pos) {
num |= (1 << pos);
}
// Clear a bit
void clearBit(int &num, int pos) {
num &= ~(1 << pos);
}
// Toggle a bit
void toggleBit(int &num, int pos) {
num ^= (1 << pos);
}
// Check a bit
bool checkBit(int num, int pos) {
return (num & (1 << pos)) != 0;
}
// Get a bitmask of multiple bits
int getBitmask(int num, int start, int end) {
int mask = (1 << (end - start + 1)) - 1;
return (num >> start) & mask;
}
};
int main() {
Bitmask bm;
int number = 0;
bm.setBit(number, 1);
std::cout << "Set bit 1: " << number << std::endl;
bm.clearBit(number, 1);
std::cout << "Clear bit 1: " << number << std::endl;
bm.toggleBit(number, 1);
std::cout << "Toggle bit 1: " << number << std::endl;
bool isSet = bm.checkBit(number, 1);
std::cout << "Check bit 1: " << isSet << std::endl;
int bitmask = bm.getBitmask(0b111110, 1, 4);
std::cout << "Get bitmask from bit 1 to 4: " << bitmask << std::endl;
return 0;
}