From 8f3c894ee9a790f1df0d0664fa71404f1b5eacd8 Mon Sep 17 00:00:00 2001 From: Mike Date: Fri, 19 Feb 2021 07:45:21 +0000 Subject: [PATCH] Fix BitSet `&` operator, add XOR operator (#2220) --- Sming/Core/Data/BitSet.h | 14 +++++++++++++- tests/HostTests/modules/BitSet.cpp | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Sming/Core/Data/BitSet.h b/Sming/Core/Data/BitSet.h index 10dd5164ad..ee7a0b47e1 100644 --- a/Sming/Core/Data/BitSet.h +++ b/Sming/Core/Data/BitSet.h @@ -377,7 +377,7 @@ template class BitSet template inline constexpr BitSet operator&(const BitSet& x, const BitSet& y) { - return BitSet(S(x) & ~S(y)); + return BitSet(S(x) & S(y)); } template @@ -416,6 +416,18 @@ inline constexpr BitSet operator-(const BitSet& x, E b return BitSet(S(x) & ~BitSet::bitVal(b)); } +template +inline constexpr BitSet operator^(BitSet x, BitSet y) +{ + return BitSet(S(x) ^ S(y)); +} + +template +inline constexpr BitSet operator^(BitSet x, E b) +{ + return x ^ BitSet(b); +} + /* * These allow construction of a maximally-sized BitSet in an expression, * which is then copy-constructed to the actual value. For example: diff --git a/tests/HostTests/modules/BitSet.cpp b/tests/HostTests/modules/BitSet.cpp index cfb5b5b2d3..331457e5f4 100644 --- a/tests/HostTests/modules/BitSet.cpp +++ b/tests/HostTests/modules/BitSet.cpp @@ -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());