Skip to content

Instantly share code, notes, and snippets.

@MatheusFaria
Created November 21, 2018 17:54
Show Gist options
  • Save MatheusFaria/4d103f42d7c044ee64ba71e45c7b8805 to your computer and use it in GitHub Desktop.
Save MatheusFaria/4d103f42d7c044ee64ba71e45c7b8805 to your computer and use it in GitHub Desktop.
32 Bitmask helper
#include <iostream>
class Bitmask {
public:
Bitmask(uint32_t _=0u) : n(_) {}
inline uint32_t get(int i) const { return (n >> i) & 1; }
inline void set (int i) { n |= (1 << i); }
inline void reset(int i) { n &= ~(1 << i); }
inline void flip (int i) { n ^= (1 << i); }
private:
uint32_t n;
friend std::ostream & operator<<(std::ostream & os, const Bitmask & B) {
for(int i = 31; i >= 0; --i) os << B.get(i);
return os;
}
friend std::istream & operator>>(std::istream & is, Bitmask & B) {
uint32_t n;
is >> n;
B.n = n;
return is;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment