Skip to content

Commit

Permalink
feat: add AreAnyBitsSet() and AreAllBitsSet() (#823)
Browse files Browse the repository at this point in the history
Add AreAnyBitsSet and AreAllBitsSet
  • Loading branch information
fabiangottstein authored Feb 13, 2025
1 parent 60a0b42 commit f83c37b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions infra/util/BitLogic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ namespace infra
return (dataRegister & Bit<T>(index)) != 0;
}

template<class T, class U>
bool AreAnyBitsSet(T dataRegister, const U& mask)
{
return (dataRegister & mask) != 0;
}

template<class T, class U>
bool AreAllBitsSet(T dataRegister, const U& mask)
{
return (dataRegister & mask) == mask;
}

template<class T>
T GetBits(T dataRegister, uint8_t size, uint8_t position)
{
Expand Down
21 changes: 21 additions & 0 deletions infra/util/test/TestBitLogic.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "infra/util/BitLogic.hpp"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

TEST(BitLogicTest, Bit)
Expand Down Expand Up @@ -52,3 +53,23 @@ TEST(BitLogicTest, GetBits)
uint32_t x = 2;
EXPECT_EQ(2, infra::GetBits(x, 1, 1));
}

TEST(BitLogicTest, AreAnyBitsSet)
{
const uint32_t x = 0b10000001;
const uint32_t y = 0b10000000;
const uint32_t mask = 0b1001;

EXPECT_THAT(infra::AreAnyBitsSet(x, mask), testing::IsTrue());
EXPECT_THAT(infra::AreAnyBitsSet(y, mask), testing::IsFalse());
}

TEST(BitLogicTest, AreAllBitsSet)
{
const uint32_t x = 0b10001001;
const uint32_t y = 0b10001000;
const uint32_t mask = 0b1001;

EXPECT_THAT(infra::AreAllBitsSet(x, mask), testing::IsTrue());
EXPECT_THAT(infra::AreAllBitsSet(y, mask), testing::IsFalse());
}

0 comments on commit f83c37b

Please sign in to comment.