10#ifndef MAXMATRIX_SRC_UTILITY_BIT_H_
11#define MAXMATRIX_SRC_UTILITY_BIT_H_
43template <
typename Integer>
47 static_assert(
static_cast<Integer
>(-1) >
static_cast<Integer
>(1),
48 "Bit: Integer is an unsigned integer other than bool");
60 static constexpr Integer
ToMask(uint8_t position) {
61 return ShiftUp(Integer{1u}, position);
75 static constexpr Integer
Combine(Integer first, Integer second) {
76 return static_cast<Integer
>(
static_cast<ShiftType
>(first) | second);
87 static constexpr bool ReadBit(Integer value, uint8_t position) {
99 static constexpr Integer
WriteBit(Integer value, uint8_t position,
bool set) {
112 return static_cast<Integer
>(
static_cast<ShiftType
>(value) & mask);
137 static constexpr Integer
ToggleBits(Integer value, Integer mask) {
138 return static_cast<Integer
>(
static_cast<ShiftType
>(value) ^ mask);
147 static constexpr Integer
Invert(Integer value) {
149 return static_cast<Integer
>(~static_cast<ShiftType>(value));
158 static constexpr Integer
ShiftUp(Integer value, uint8_t places) {
159 return static_cast<Integer
>(
static_cast<ShiftType
>(value) << places);
168 static constexpr Integer
ShiftDown(Integer value, uint8_t places) {
169 return static_cast<Integer
>(
static_cast<ShiftType
>(value) >> places);
179 static constexpr Integer
RotateLeft(Integer value, uint8_t places) {
181 ShiftDown(value,
static_cast<uint8_t
>(kWidth - places)));
200 template <
typename Target>
201 static void SetBits(Target& target, Integer mask) {
215 template <
typename Target>
223 using ShiftType =
decltype(Integer{} | 0u);
227 static constexpr uint8_t kBitsPerByte{8u};
228 static constexpr uint8_t kWidth{
229 static_cast<uint8_t
>(
sizeof(Integer) * kBitsPerByte)};
static constexpr Integer ShiftUp(Integer value, uint8_t places)
Returns value moved places up; the bits beyond the top fall out.
Definition Bit.h:158
static constexpr bool ReadBit(Integer value, uint8_t position)
Returns whether the bit at position of value is set.
Definition Bit.h:87
static constexpr Integer WriteBit(Integer value, uint8_t position, bool set)
Returns value with the bit at position set or cleared.
Definition Bit.h:99
static constexpr Integer ShiftDown(Integer value, uint8_t places)
Returns value moved places down; the bits below bit 0 fall out.
Definition Bit.h:168
static constexpr Integer Combine(Integer first, Integer second)
Returns the bits set in either of two values: the mask of a group from the masks of its parts,...
Definition Bit.h:75
static constexpr Integer RotateLeft(Integer value, uint8_t places)
Returns value moved places up, the bits beyond the top coming in again at the bottom.
Definition Bit.h:179
static constexpr Integer ToMask(uint8_t position)
Returns the mask of the bit at position.
Definition Bit.h:60
static void SetBits(Target &target, Integer mask)
Sets the bits of mask in target, the other bits left as they were.
Definition Bit.h:201
static constexpr Integer ReadBitGroup(Integer value, Integer mask)
Returns the bits of value that mask selects, where they stand.
Definition Bit.h:111
static constexpr Integer ReadBitGroup(Integer value, Integer mask, uint8_t position)
Returns the group of bits of value that starts at position, shifted down to bit 0.
Definition Bit.h:124
static void ClearBits(Target &target, Integer mask)
Clears the bits of mask in target, the other bits left as they were.
Definition Bit.h:216
Bit()=delete
Deleted: the functions are static, no Bit is made.
static constexpr Integer Invert(Integer value)
Returns value with every bit inverted.
Definition Bit.h:147
static constexpr Integer ToggleBits(Integer value, Integer mask)
Returns value with the bits of mask inverted, the others as they were.
Definition Bit.h:137