Created
November 21, 2018 17:54
-
-
Save MatheusFaria/4d103f42d7c044ee64ba71e45c7b8805 to your computer and use it in GitHub Desktop.
32 Bitmask helper
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
#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