Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (MemoryUtils): Add combineTwoBytes function #815

Merged
merged 1 commit into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = static_cast<uint16_t>(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);
}