Created
November 25, 2021 07:41
-
-
Save HirbodBehnam/bf6bccf71115c4b3147d92a5bd64a9b2 to your computer and use it in GitHub Desktop.
Ultra simple implementation of bit set in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class bit_set { | |
private: | |
uint8_t *data; | |
public: | |
explicit bit_set(int size) { | |
// (size + 7) / 8 is ceiling of size / 8 | |
data = new uint8_t[(size + 7) / 8](); | |
} | |
void set(int index) { | |
data[index / 8] |= 1 << (index % 8); | |
} | |
void del(int index) { | |
data[index / 8] &= ~(1 << (index % 8)); | |
} | |
bool is_set(int index) const { | |
return (data[index / 8] & (1 << (index % 8))) != 0; | |
} | |
~bit_set() { | |
delete data; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment