diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2026-03-20 17:59:56 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2026-03-20 17:59:56 +0100 |
| commit | 77de9119efbee9e65f0b50fc8b9315b6fb0910fb (patch) | |
| tree | 17a335925106f44e6b3178c8c61beba583d3b42d /bitint_wrapper.h | |
| parent | d16656919753438a57a32f9ad6ba972c25d66b11 (diff) | |
| download | zmodn-77de9119efbee9e65f0b50fc8b9315b6fb0910fb.tar.gz zmodn-77de9119efbee9e65f0b50fc8b9315b6fb0910fb.zip | |
Diffstat (limited to 'bitint_wrapper.h')
| -rw-r--r-- | bitint_wrapper.h | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/bitint_wrapper.h b/bitint_wrapper.h new file mode 100644 index 0000000..785d0fa --- /dev/null +++ b/bitint_wrapper.h | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | #ifndef BITINT_WRAPPER_H | ||
| 2 | #define BITINT_WRAPPER_H | ||
| 3 | |||
| 4 | #include <cstdio> | ||
| 5 | #include <iostream> | ||
| 6 | |||
| 7 | // Wrapper class for _BitInt(N) (port of the C23 feature, compiler | ||
| 8 | // extension for C++ available in Clang and maybe GCC). This is needed | ||
| 9 | // because operator<< is not defined on _BitInt(N), apparently. | ||
| 10 | |||
| 11 | template<uint64_t N = 128> | ||
| 12 | class BitInt { | ||
| 13 | public: | ||
| 14 | _BitInt(N) x; | ||
| 15 | |||
| 16 | constexpr BitInt() : x{_BitInt(N)(0)} {} | ||
| 17 | constexpr BitInt(int n) : x{_BitInt(N)(n)} {} | ||
| 18 | constexpr BitInt(long long n) : x{_BitInt(N)(n)} {} | ||
| 19 | constexpr BitInt(_BitInt(N) n) : x{n} {} | ||
| 20 | constexpr auto operator<=>(const BitInt& b) const { return x <=> b.x; } | ||
| 21 | constexpr bool operator==(const BitInt& b) const = default; | ||
| 22 | constexpr BitInt operator+(const BitInt& b) const { return x + b.x; } | ||
| 23 | constexpr BitInt operator-(const BitInt& b) const { return x - b.x; } | ||
| 24 | constexpr BitInt operator*(const BitInt& b) const { return x * b.x; } | ||
| 25 | constexpr BitInt operator/(const BitInt& b) const { return x / b.x; } | ||
| 26 | constexpr BitInt operator%(const BitInt& b) const { return x % b.x; } | ||
| 27 | constexpr BitInt operator-() const { return -x; } | ||
| 28 | constexpr BitInt operator+=(const BitInt& b) { return *this = *this + b; } | ||
| 29 | constexpr BitInt operator-=(const BitInt& b) { return *this = *this - b; } | ||
| 30 | constexpr BitInt operator*=(const BitInt& b) { return *this = *this * b; } | ||
| 31 | constexpr BitInt operator/=(const BitInt& b) { return *this = *this / b; } | ||
| 32 | constexpr BitInt operator%=(const BitInt& b) { return *this = *this % b; } | ||
| 33 | |||
| 34 | friend std::ostream& operator<<(std::ostream& os, const BitInt<N>& b) { | ||
| 35 | if (b > 0) { | ||
| 36 | std::string s; | ||
| 37 | auto bb = b; | ||
| 38 | while (bb != 0) { | ||
| 39 | char c = (bb.x % 10) + '0'; | ||
| 40 | s = c + s; | ||
| 41 | bb /= 10; | ||
| 42 | } | ||
| 43 | return os << s; | ||
| 44 | } else if (b < 0) { | ||
| 45 | return os << "-" << -b; | ||
| 46 | } else return os << "0"; | ||
| 47 | } | ||
| 48 | }; | ||
| 49 | |||
| 50 | #endif | ||
