Bit Pattern DSL
#include <cstdint>
#include <stdexcept>
class BitPattern
{
private:
std::uint64_t expected{};
std::uint64_t mask{0XFFFFFFFFFFFFFFFF};
public:
template<std::size_t Size>
explicit constexpr BitPattern(const char (&input)[Size])
{
std::uint64_t cur_bit = (1 << (Size - 2));
for(const char val : input)
{
if(val == 0){
return;
}
if(val == '1')
{
expected |= cur_bit;
}
else if(val == '0')
{
}
else if(val == 'x' || val == 'X')
{
mask &= ~cur_bit;
}
else
{
throw std::logic_error("Unknow ....");
}
cur_bit >>= 1;
}
}
constexpr friend bool operator==(const std::uint64_t value, const BitPattern& pattern)
{
return (value & pattern.mask) == pattern.expected;
}
};
int main()
{
static_assert(0b110101 == BitPattern("11XX01"));
}