codex
Loading...
Searching...
No Matches
bit_pattern.hpp
Go to the documentation of this file.
1#ifndef CODEX_BIT_PATTERN_H
2#define CODEX_BIT_PATTERN_H
3
4#include <concepts>
5#include <cstddef>
6#include <cstdint>
7#include <stdexcept>
8
9class BitPattern {
10public:
11 template<std::size_t N>
12 explicit consteval BitPattern(const char (&pattern)[N]) {
13 for(const auto *c = pattern; *c; ++c) {
14 this->mask <<= 1;
15 this->expected <<= 1;
16 switch(*c) {
17 case 'x':
18 case 'X': break;
19 case '1': this->expected |= 1; [[fallthrough]];
20 case '0': this->mask |= 1; break;
21 default: throw std::domain_error("invalid input");
22 }
23 }
24 }
25
26 constexpr bool matches(std::unsigned_integral auto v)
27 { return (v & this->mask) == this->expected; }
28private:
29 std::uint64_t mask = {}, expected = {};
30};
31
32#endif
Definition bit_pattern.hpp:9
std::uint64_t mask
Definition bit_pattern.hpp:29
consteval BitPattern(const char(&pattern)[N])
Definition bit_pattern.hpp:12
constexpr bool matches(std::unsigned_integral auto v)
Definition bit_pattern.hpp:26
std::uint64_t expected
Definition bit_pattern.hpp:29
char c[2][6]
Definition std2.c:4
Definition main.cpp:7
Definition mult_inh.c:27