Please consider the code example where I want to check if all bits in a unsigned integer variable are set. IntegerType
is replaced by uint8_t, uint16_t, uint32_t uint64_t.
The question: Why does the assertion succeed for IntegerType = uint32_t and uint64_t while it fails for uint16_t and uint8_t?
#include <cstdint>
#include <cassert>
IntegerType bitset = -1; // set all bits to true
IntegerType t = ~bitset;
bool bAllBitsSet1 = (t == 0);
bool bAllBitsSet2 = ((~bitset) == 0);
assert(bAllBitsSet1 == bAllBitsSet2);