MaxMatrix
Drive chained 8x8 LED dot matrix modules with MAX7219.
Loading...
Searching...
No Matches
Bit.h
Go to the documentation of this file.
1// Copyright 2026 Andreas Burnickl
2// SPDX-License-Identifier: GPL-3.0-or-later
3
9
10#ifndef MAXMATRIX_SRC_UTILITY_BIT_H_
11#define MAXMATRIX_SRC_UTILITY_BIT_H_
12
13#include <stdint.h>
14
15namespace max_matrix {
16
38/*
39 * MaxMatrix's own, by CodingStyle.md M-3: it depends on neither the ETL nor
40 * Utl, whose utl::Bit this follows with only what is used here, the static
41 * functions; no value is kept bit by bit.
42 */
43template <typename Integer>
44class Bit {
45 // -1 converted is the largest value of an unsigned type only: in a signed
46 // one it stays -1, and in a bool it is true, as 1 is.
47 static_assert(static_cast<Integer>(-1) > static_cast<Integer>(1),
48 "Bit: Integer is an unsigned integer other than bool");
49
50 public:
52 Bit() = delete;
53
60 static constexpr Integer ToMask(uint8_t position) {
61 return ShiftUp(Integer{1u}, position);
62 }
63
75 static constexpr Integer Combine(Integer first, Integer second) {
76 return static_cast<Integer>(static_cast<ShiftType>(first) | second);
77 }
78
85 // The value shifted down to the bit rather than a mask shifted up to it, as
86 // utl::Bit does: 26 bytes less in Basics on the Uno.
87 static constexpr bool ReadBit(Integer value, uint8_t position) {
88 return ReadBitGroup(ShiftDown(value, position), Integer{1u}) != Integer{0u};
89 }
90
99 static constexpr Integer WriteBit(Integer value, uint8_t position, bool set) {
100 return set ? Combine(value, ToMask(position))
101 : ReadBitGroup(value, Invert(ToMask(position)));
102 }
103
111 static constexpr Integer ReadBitGroup(Integer value, Integer mask) {
112 return static_cast<Integer>(static_cast<ShiftType>(value) & mask);
113 }
114
124 static constexpr Integer ReadBitGroup(Integer value, Integer mask,
125 uint8_t position) {
126 return ReadBitGroup(ShiftDown(value, position), mask);
127 }
128
137 static constexpr Integer ToggleBits(Integer value, Integer mask) {
138 return static_cast<Integer>(static_cast<ShiftType>(value) ^ mask);
139 }
140
147 static constexpr Integer Invert(Integer value) {
148 // ~ of an Integer narrower than int would be a negative int.
149 return static_cast<Integer>(~static_cast<ShiftType>(value));
150 }
151
158 static constexpr Integer ShiftUp(Integer value, uint8_t places) {
159 return static_cast<Integer>(static_cast<ShiftType>(value) << places);
160 }
161
168 static constexpr Integer ShiftDown(Integer value, uint8_t places) {
169 return static_cast<Integer>(static_cast<ShiftType>(value) >> places);
170 }
171
179 static constexpr Integer RotateLeft(Integer value, uint8_t places) {
180 return Combine(ShiftUp(value, places),
181 ShiftDown(value, static_cast<uint8_t>(kWidth - places)));
182 }
183
200 template <typename Target>
201 static void SetBits(Target& target, Integer mask) {
202 target |= mask;
203 }
204
215 template <typename Target>
216 static void ClearBits(Target& target, Integer mask) {
217 target &= Invert(mask);
218 }
219
220 private:
221 // What Integer is shifted in: Integer itself when it is at least an
222 // unsigned int, unsigned int otherwise.
223 using ShiftType = decltype(Integer{} | 0u);
224
225 // Not CHAR_BIT: clang-tidy, with the board's flags, finds the host's
226 // <limits.h>, which does not parse there.
227 static constexpr uint8_t kBitsPerByte{8u};
228 static constexpr uint8_t kWidth{
229 static_cast<uint8_t>(sizeof(Integer) * kBitsPerByte)};
230};
231
232} // namespace max_matrix
233
234#endif // MAXMATRIX_SRC_UTILITY_BIT_H_
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