Skip to content

Commit

Permalink
✨ (MemoryUtils): Add combineTwoBytes function
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed May 30, 2022
1 parent 3286427 commit 2e32ee4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions libs/Utils/include/MemoryUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,20 @@ constexpr auto getHighByte(uint16_t value) noexcept -> uint8_t
return static_cast<uint8_t>(value >> 8);
}

struct TwoBytes {
uint8_t high {};
uint8_t low {};
};

constexpr auto combineBytes(TwoBytes bytes) noexcept -> uint16_t
{
auto both = uint16_t {};

both = bytes.high;
both = both << 8;
both |= bytes.low;

return both;
}

} // namespace leka::utils::memory
16 changes: 16 additions & 0 deletions libs/Utils/tests/MemoryUtils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,19 @@ TEST(MemoryUtilsTest, getHighByte)

ASSERT_EQ(0b1111'1111, high);
}

TEST(MemoryUtilsTest, combineTwoBytes)
{
auto high = uint8_t {0xAB};
auto low = uint8_t {0xCD};

auto both = combineBytes({.high = high, .low = low});

EXPECT_EQ(both, 0xABCD);

auto new_high = getHighByte(both);
auto new_low = getLowByte(both);

EXPECT_EQ(new_high, high);
EXPECT_EQ(new_low, low);
}

0 comments on commit 2e32ee4

Please sign in to comment.