Created
September 10, 2021 11:57
-
-
Save Andersama/03a84c4c9d12a141b3f2e654244874a5 to your computer and use it in GitHub Desktop.
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
| #pragma once | |
| #include <cstdint> | |
| struct flags_t { | |
| uint32_t bitmask = {}; | |
| // set the bit at a particular index | |
| constexpr void set(uint8_t index) noexcept { | |
| bitmask |= 1UL << index; | |
| } | |
| // unset the bit at a particular index | |
| constexpr void unset(uint8_t index) noexcept { | |
| uint32_t v = ~(1UL << index); | |
| bitmask &= v; | |
| } | |
| // flip the bit at a particular index | |
| constexpr void flip(uint8_t index) noexcept { | |
| bitmask = bitmask ^ (1UL << index); | |
| } | |
| // set the set bits of another bitmask | |
| constexpr void set(flags_t f) noexcept { | |
| bitmask |= f.bitmask; | |
| } | |
| // unset the set bits of another bitmask | |
| constexpr void unset(flags_t f) noexcept { | |
| bitmask &= ~f.bitmask; | |
| } | |
| // unset all bits (set to 0) | |
| constexpr void clear() noexcept { | |
| bitmask = 0UL; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment