Skip to content

Instantly share code, notes, and snippets.

@HirbodBehnam
Created November 25, 2021 07:41
Show Gist options
  • Save HirbodBehnam/bf6bccf71115c4b3147d92a5bd64a9b2 to your computer and use it in GitHub Desktop.
Save HirbodBehnam/bf6bccf71115c4b3147d92a5bd64a9b2 to your computer and use it in GitHub Desktop.
Ultra simple implementation of bit set in C++
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