Skip to content

Instantly share code, notes, and snippets.

@Jakz
Last active August 29, 2015 14:14
Show Gist options
  • Save Jakz/8f93a11cbebbe5a3d3b0 to your computer and use it in GitHub Desktop.
Save Jakz/8f93a11cbebbe5a3d3b0 to your computer and use it in GitHub Desktop.
Simple C++11 BitSet
template<size_t S, typename T>
class SimpleBitSet
{
private:
T data[S/(sizeof(T)*8)];
public:
const size_t BITS = sizeof(T)*8;
const size_t SIZE = S/BITS;
SimpleBitSet() : data{0} { }
inline void set(size_t index) { data[index/BITS] |= (1 << (index%BITS)); }
inline void reset(size_t index) { data[index/BITS] &= ~(1 << (index%BITS)); }
inline void clear() { memset(data, 0, SIZE*sizeof(T)); }
inline bool isSet(size_t index) const { return (data[index/BITS] & (1 << (index%BITS))) != 0; }
inline void getData(T (&data)[S/(sizeof(T)*8)]) const {
memcpy(data, this->data, SIZE*sizeof(T));
}
inline void setData(const T (&data)[S/(sizeof(T)*8)]) {
memcpy(this->data, data, SIZE*sizeof(T));
}
static constexpr size_t realSize() { return S / (sizeof(T) * 8); }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment