MaxMatrix
Drive chained 8x8 LED dot matrix modules with MAX7219.
Loading...
Searching...
No Matches
max_matrix::Bit< Integer > Class Template Reference

The bits of an unsigned integer, read and written by static functions, as a register, a byte of a frame or a word of a hash. More...

#include <Bit.h>

Public Member Functions

 Bit ()=delete
 Deleted: the functions are static, no Bit is made.

Static Public Member Functions

static constexpr Integer ToMask (uint8_t position)
 Returns the mask of the bit at position.
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, or a value put together from its parts.
static constexpr bool ReadBit (Integer value, uint8_t position)
 Returns whether the bit at position of value is set.
static constexpr Integer WriteBit (Integer value, uint8_t position, bool set)
 Returns value with the bit at position set or cleared.
static constexpr Integer ReadBitGroup (Integer value, Integer mask)
 Returns the bits of value that mask selects, where they stand.
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.
static constexpr Integer ToggleBits (Integer value, Integer mask)
 Returns value with the bits of mask inverted, the others as they were.
static constexpr Integer Invert (Integer value)
 Returns value with every bit inverted.
static constexpr Integer ShiftUp (Integer value, uint8_t places)
 Returns value moved places up; the bits beyond the top fall out.
static constexpr Integer ShiftDown (Integer value, uint8_t places)
 Returns value moved places down; the bits below bit 0 fall out.
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.
template<typename Target>
static void SetBits (Target &target, Integer mask)
 Sets the bits of mask in target, the other bits left as they were.
template<typename Target>
static void ClearBits (Target &target, Integer mask)
 Clears the bits of mask in target, the other bits left as they were.

Detailed Description

template<typename Integer>
class max_matrix::Bit< Integer >

The bits of an unsigned integer, read and written by static functions, as a register, a byte of a frame or a word of a hash.

bool const lit{max_matrix::Bit<uint8_t>::ReadBit(column, 3)};
static constexpr bool ReadBit(Integer value, uint8_t position)
Returns whether the bit at position of value is set.
Definition Bit.h:87
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

A position counts from 0, the least significant bit. A group of bits is given by its mask as it stands in the value; ReadBitGroup() with a position is for a mask written down unshifted, as a datasheet gives a field.

A value is shifted as an unsigned int where Integer is narrower: a uint8_t would be promoted to an int, which has a sign and, on the AVR, 16 bits.

Template Parameters
IntegerThe unsigned integer, as uint8_t or uint32_t; not bool.
Precondition
Each position and number of places lies below the width of Integer; a shift by as much is undefined.

Member Function Documentation

◆ ClearBits()

template<typename Integer>
template<typename Target>
void max_matrix::Bit< Integer >::ClearBits ( Target & target,
Integer mask )
inlinestatic

Clears the bits of mask in target, the other bits left as they were.

Template Parameters
TargetThe type of the target, as for SetBits().
Parameters
[in,out]targetThe register or variable to change.
[in]maskThe bits to clear.
Warning
It reads the target, changes it and writes it back, and not atomically; see SetBits().

◆ Combine()

template<typename Integer>
constexpr Integer max_matrix::Bit< Integer >::Combine ( Integer first,
Integer second )
inlinestaticconstexpr

Returns the bits set in either of two values: the mask of a group from the masks of its parts, or a value put together from its parts.

What a | of named masks does in a constexpr, the one place it may stand outside this class (docs/CodingStyle.md H-9); this anywhere.

Parameters
[in]firstThe one value.
[in]secondThe other.
Returns
Every bit that is set in one of them.

◆ Invert()

template<typename Integer>
constexpr Integer max_matrix::Bit< Integer >::Invert ( Integer value)
inlinestaticconstexpr

Returns value with every bit inverted.

Parameters
[in]valueThe integer.
Returns
The bits that are not set in value.

◆ ReadBit()

template<typename Integer>
constexpr bool max_matrix::Bit< Integer >::ReadBit ( Integer value,
uint8_t position )
inlinestaticconstexpr

Returns whether the bit at position of value is set.

Parameters
[in]valueThe integer to read.
[in]positionThe position of the bit.

◆ ReadBitGroup() [1/2]

template<typename Integer>
constexpr Integer max_matrix::Bit< Integer >::ReadBitGroup ( Integer value,
Integer mask )
inlinestaticconstexpr

Returns the bits of value that mask selects, where they stand.

Parameters
[in]valueThe integer to read.
[in]maskThe group as it stands in the value.
Returns
The bits of the group in place, every other bit cleared.

◆ ReadBitGroup() [2/2]

template<typename Integer>
constexpr Integer max_matrix::Bit< Integer >::ReadBitGroup ( Integer value,
Integer mask,
uint8_t position )
inlinestaticconstexpr

Returns the group of bits of value that starts at position, shifted down to bit 0.

Parameters
[in]valueThe integer to read.
[in]maskThe group written down unshifted, from bit 0 on.
[in]positionThe position of the group's lowest bit in value.
Returns
The bits of the group from bit 0 on, every other bit cleared.

◆ RotateLeft()

template<typename Integer>
constexpr Integer max_matrix::Bit< Integer >::RotateLeft ( Integer value,
uint8_t places )
inlinestaticconstexpr

Returns value moved places up, the bits beyond the top coming in again at the bottom.

Parameters
[in]valueThe integer.
[in]placesThe number of places, 1 to the width less 1.

◆ SetBits()

template<typename Integer>
template<typename Target>
void max_matrix::Bit< Integer >::SetBits ( Target & target,
Integer mask )
inlinestatic

Sets the bits of mask in target, the other bits left as they were.

Template Parameters
TargetThe type of the target: uint8_t volatile for a port, an Integer for a variable, or what stands for a port with |= and &= of its own, as the mock of the tests does, so that every bit reaches its pin there.
Parameters
[in,out]targetThe register or variable to change.
[in]maskThe bits to set.
Warning
It reads the target, changes it and writes it back, as |= on a register does, and not atomically: a target that an interrupt writes as well needs the caller's guard. Not for a register of flags, where writing back a 1 that was read clears a flag, nor for PINB and its like, where a 1 written toggles the pin.

◆ ShiftDown()

template<typename Integer>
constexpr Integer max_matrix::Bit< Integer >::ShiftDown ( Integer value,
uint8_t places )
inlinestaticconstexpr

Returns value moved places down; the bits below bit 0 fall out.

Parameters
[in]valueThe integer.
[in]placesThe number of places.

◆ ShiftUp()

template<typename Integer>
constexpr Integer max_matrix::Bit< Integer >::ShiftUp ( Integer value,
uint8_t places )
inlinestaticconstexpr

Returns value moved places up; the bits beyond the top fall out.

Parameters
[in]valueThe integer.
[in]placesThe number of places.

◆ ToggleBits()

template<typename Integer>
constexpr Integer max_matrix::Bit< Integer >::ToggleBits ( Integer value,
Integer mask )
inlinestaticconstexpr

Returns value with the bits of mask inverted, the others as they were.

Parameters
[in]valueThe integer to change.
[in]maskThe bits to invert.
Returns
The changed integer.

◆ ToMask()

template<typename Integer>
constexpr Integer max_matrix::Bit< Integer >::ToMask ( uint8_t position)
inlinestaticconstexpr

Returns the mask of the bit at position.

Parameters
[in]positionThe position of the bit.
Returns
An integer with that bit set and every other bit cleared.

◆ WriteBit()

template<typename Integer>
constexpr Integer max_matrix::Bit< Integer >::WriteBit ( Integer value,
uint8_t position,
bool set )
inlinestaticconstexpr

Returns value with the bit at position set or cleared.

Parameters
[in]valueThe integer to change.
[in]positionThe position of the bit.
[in]setTrue to set the bit, false to clear it.
Returns
The changed integer.

The documentation for this class was generated from the following file: