Skip to content

Commit

Permalink
✨ Add >>/<< shifts to bit masks (#50)
Browse files Browse the repository at this point in the history
- ✅ Add & fix bit_value tests.
- ✅ Add unit tests for the bit mask library.
  • Loading branch information
kammce authored Dec 21, 2024
1 parent 5c062a5 commit 7726c14
Show file tree
Hide file tree
Showing 3 changed files with 347 additions and 248 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set(TEST_SOURCES_LIST
tests/atomic_spin_lock.test.cpp
tests/can.test.cpp
tests/bit.test.cpp
tests/bit.functions.test.cpp
tests/enum.test.cpp
tests/i2c.test.cpp
tests/input_pin.test.cpp
Expand Down
57 changes: 54 additions & 3 deletions include/libhal-util/bit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include <concepts>
#include <limits>

// NOTE: DO not include libhal/units.hpp here. We want this file to stay as a
// dropp in header only library that just uses C++'s stdlib.

/**
* @defgroup Bit Bit Operations
*
Expand Down Expand Up @@ -173,13 +176,52 @@ struct bit_mask
* @ingroup Bit
* @brief Comparison operator between this mask and another
*
* @param other - the other mask to compare against
* @param p_other - the other mask to compare against
* @return true - the masks are the same
* @return false - the masks are not the same
*/
constexpr bool operator==(bit_mask const& other)
constexpr bool operator==(bit_mask const& p_other)
{
return p_other.position == position && p_other.width == width;
}

/**
* @ingroup Bit
* @brief Shift the position of the bit mask to the right
*
* NOTE: the position will overflow if the position
*
* @param p_shift_amount - the number of bits to shift the position by
* @return constexpr auto - a copy of this bit_mask but with the position
* shifted.
*/
constexpr auto operator>>(std::uint32_t p_shift_amount) const
{
hal::bit_mask result = *this;
if (result.position > p_shift_amount) {
result.position -= p_shift_amount;
} else {
result.position = 0;
}
return result;
}

/**
* @ingroup Bit
* @brief Shift the position of the bit mask to the left
*
* NOTE: the position will not underflow if the shift amount is greater than
* the position. The position value will saturate at the value of 0.
*
* @param p_shift_amount - the number of bits to shift the position by
* @return constexpr auto - a copy of this bit_mask but with the position
* shifted.
*/
constexpr auto operator<<(std::uint32_t p_shift_amount) const
{
return other.position == position && other.width == width;
hal::bit_mask result = *this;
result.position += p_shift_amount;
return result;
}
};

Expand Down Expand Up @@ -389,6 +431,15 @@ class bit_value
: m_value(p_initial_value)
{
}
/**
* @brief Constructs a new bit_value instance with an initial value.
*
* @param p_initial_value The initial value to use. Defaults to 0.
*/
constexpr bit_value& operator=(T p_initial_value)
{
m_value = p_initial_value;
}

/**
* @brief Sets (sets to a 1) multiple bits in the represented value.
Expand Down
Loading

0 comments on commit 7726c14

Please sign in to comment.