Skip to content

Instantly share code, notes, and snippets.

@Andersama
Created September 10, 2021 11:57
Show Gist options
  • Select an option

  • Save Andersama/03a84c4c9d12a141b3f2e654244874a5 to your computer and use it in GitHub Desktop.

Select an option

Save Andersama/03a84c4c9d12a141b3f2e654244874a5 to your computer and use it in GitHub Desktop.
#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