Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstdint>
- class Bitmap {
- private:
- std::vector<uint8_t> bits; // byte array (each byte = 8 bits)
- size_t size; // total number of bits
- public:
- // Constructor
- Bitmap(size_t numBits) : size(numBits) {
- bits.resize((numBits + 7) / 8, 0); // ceil(numBits/8) bytes
- }
- // Set bit at index
- void set(size_t index) {
- if (index >= size) return;
- bits[index / 8] |= (1 << (index % 8));
- }
- // Clear bit at index
- void clear(size_t index) {
- if (index >= size) return;
- bits[index / 8] &= ~(1 << (index % 8));
- }
- // Get bit at index
- bool get(size_t index) const {
- if (index >= size) return false;
- return bits[index / 8] & (1 << (index % 8));
- }
- // Print all bits
- void printBits() const {
- for (size_t i = 0; i < size; ++i) {
- std::cout << get(i);
- }
- std::cout << std::endl;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement