diff --git a/src/core/common/num_utils.hpp b/src/core/common/num_utils.hpp index 8f112d4c6..70c0fcf62 100644 --- a/src/core/common/num_utils.hpp +++ b/src/core/common/num_utils.hpp @@ -272,7 +272,7 @@ template void SetBit(UintType &aBits, uint8_t aBitOffset) { static_assert(TypeTraits::IsUint::kValue, "UintType must be an unsigned int (8, 16, 32, or 64 bit len)"); - aBits = aBits | static_cast(static_cast(1) << aBitOffset); + aBits |= static_cast(static_cast(1) << aBitOffset); } /** @@ -288,7 +288,7 @@ template void ClearBit(UintType &aBits, uint8_t aBitOffset) { static_assert(TypeTraits::IsUint::kValue, "UintType must be an unsigned int (8, 16, 32, or 64 bit len)"); - aBits = aBits & (~(static_cast(1) << aBitOffset)); + aBits &= (~(static_cast(1) << aBitOffset)); } /** diff --git a/src/core/thread/network_data_tlvs.hpp b/src/core/thread/network_data_tlvs.hpp index d51a8e5f9..f2227e02e 100644 --- a/src/core/thread/network_data_tlvs.hpp +++ b/src/core/thread/network_data_tlvs.hpp @@ -42,6 +42,7 @@ #include "common/debug.hpp" #include "common/encoding.hpp" #include "common/equatable.hpp" +#include "common/num_utils.hpp" #include "net/ip6_address.hpp" #include "thread/mle_types.hpp" #include "thread/network_data_types.hpp" @@ -130,8 +131,8 @@ public: */ void Init(void) { - mType = 0; - mLength = 0; + mTypeAndStableFlag = 0; + mLength = 0; } /** @@ -139,14 +140,14 @@ public: * * @returns The Type value. */ - Type GetType(void) const { return static_cast(mType >> kTypeOffset); } + Type GetType(void) const { return static_cast(ReadBits(mTypeAndStableFlag)); } /** * Sets the Type value. * * @param[in] aType The Type value. */ - void SetType(Type aType) { mType = (mType & ~kTypeMask) | ((aType << kTypeOffset) & kTypeMask); } + void SetType(Type aType) { WriteBits(mTypeAndStableFlag, aType); } /** * Returns the Length value. @@ -221,7 +222,7 @@ public: /** * Clears the Stable bit. */ - void ClearStable(void) { mType &= ~kStableMask; } + void ClearStable(void) { ClearBit(mTypeAndStableFlag, kStableFlagOffset); } /** * Indicates whether or not the Stable bit is set. @@ -229,12 +230,12 @@ public: * @retval TRUE If the Stable bit is set. * @retval FALSE If the Stable bit is not set. */ - bool IsStable(void) const { return (mType & kStableMask); } + bool IsStable(void) const { return GetBit(mTypeAndStableFlag, kStableFlagOffset); } /** * Sets the Stable bit. */ - void SetStable(void) { mType |= kStableMask; } + void SetStable(void) { SetBit(mTypeAndStableFlag, kStableFlagOffset); } /** * Searches in a given sequence of TLVs to find the first TLV with a given type. @@ -359,13 +360,13 @@ public: } private: - static constexpr uint8_t kTypeOffset = 1; - static constexpr uint8_t kTypeMask = 0x7f << kTypeOffset; - static constexpr uint8_t kStableMask = 1 << 0; + static constexpr uint8_t kStableFlagOffset = 0; + static constexpr uint8_t kTypeOffset = 1; + static constexpr uint8_t kTypeMask = 0x7f << kTypeOffset; static bool IsTlvValid(const NetworkDataTlv *aTlv); - uint8_t mType; + uint8_t mTypeAndStableFlag; uint8_t mLength; } OT_TOOL_PACKED_END;