Bitmask Class®

Bitmask Class

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:

  • Implemented various bitmask operations such as setting, clearing, toggling, and checking bits.
  • Provided examples of efficient data handling using bitmasks.
  • Utilized C++ for powerful and efficient bitmask operations.
  • Developed practical use cases to demonstrate real-world applications of bitmasks.

Technologies Used
  • C++
  • Visual Studio
  • GitHub

Code Snippets

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;
}


Challenges and Solutions
  • Challenge: Implementing efficient bit manipulation techniques.
    Solution: Utilized core C++ capabilities to achieve optimal performance.
  • Challenge: Demonstrating practical applications of bitmasks.
    Solution: Created various examples to showcase real-world use cases.

Khaled Zakaria • Game Developer • Unreal engine Programmer • Instructor • Game Programmer •