Advertisement
thewitchking

Untitled

Jul 7th, 2025
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdint>
  4.  
  5. class Bitmap {
  6. private:
  7.     std::vector<uint8_t> bits; // byte array (each byte = 8 bits)
  8.     size_t size; // total number of bits
  9.  
  10. public:
  11.     // Constructor
  12.     Bitmap(size_t numBits) : size(numBits) {
  13.         bits.resize((numBits + 7) / 8, 0); // ceil(numBits/8) bytes
  14.     }
  15.  
  16.     // Set bit at index
  17.     void set(size_t index) {
  18.         if (index >= size) return;
  19.         bits[index / 8] |= (1 << (index % 8));
  20.     }
  21.  
  22.     // Clear bit at index
  23.     void clear(size_t index) {
  24.         if (index >= size) return;
  25.         bits[index / 8] &= ~(1 << (index % 8));
  26.     }
  27.  
  28.     // Get bit at index
  29.     bool get(size_t index) const {
  30.         if (index >= size) return false;
  31.         return bits[index / 8] & (1 << (index % 8));
  32.     }
  33.  
  34.     // Print all bits
  35.     void printBits() const {
  36.         for (size_t i = 0; i < size; ++i) {
  37.             std::cout << get(i);
  38.         }
  39.         std::cout << std::endl;
  40.     }
  41. };
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement