Skip to content

Commit

Permalink
Fix BitSet & operator, add XOR operator (#2220)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 authored Feb 19, 2021
1 parent 11836eb commit 8f3c894
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Sming/Core/Data/BitSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ template <typename S, typename E, size_t size_ = sizeof(S) * 8> class BitSet
template <typename S, typename E, size_t size_>
inline constexpr BitSet<S, E, size_> operator&(const BitSet<S, E, size_>& x, const BitSet<S, E, size_>& y)
{
return BitSet<S, E, size_>(S(x) & ~S(y));
return BitSet<S, E, size_>(S(x) & S(y));
}

template <typename S, typename E, size_t size_>
Expand Down Expand Up @@ -416,6 +416,18 @@ inline constexpr BitSet<S, E, size_> operator-(const BitSet<S, E, size_>& x, E b
return BitSet<S, E, size_>(S(x) & ~BitSet<S, E, size_>::bitVal(b));
}

template <typename S, typename E, size_t size_>
inline constexpr BitSet<S, E, size_> operator^(BitSet<S, E, size_> x, BitSet<S, E, size_> y)
{
return BitSet<S, E, size_>(S(x) ^ S(y));
}

template <typename S, typename E, size_t size_>
inline constexpr BitSet<S, E, size_> operator^(BitSet<S, E, size_> x, E b)
{
return x ^ BitSet<S, E, size_>(b);
}

/*
* These allow construction of a maximally-sized BitSet in an expression,
* which is then copy-constructed to the actual value. For example:
Expand Down
4 changes: 4 additions & 0 deletions tests/HostTests/modules/BitSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ class BitSetTest : public TestGroup
basket -= Fruit::orange;
REQUIRE(basket.value() == (_BV(Fruit::banana) | _BV(Fruit::tomato)));

basket |= Fruit::kiwi;
REQUIRE(basket.value() == (_BV(Fruit::kiwi) | _BV(Fruit::banana) | _BV(Fruit::tomato)));

basket &= fixedBasket;
REQUIRE(basket.value() == (_BV(Fruit::banana) | _BV(Fruit::tomato)));

basket = ~fixedBasket;
debug_e("basket.value = 0x%08x", basket.value());
Expand Down

0 comments on commit 8f3c894

Please sign in to comment.