aboutsummaryrefslogtreecommitdiff
path: root/bitint_wrapper.h
blob: 785d0fa331bcec93d9dc0b817d9848903201345e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef BITINT_WRAPPER_H
#define BITINT_WRAPPER_H

#include <cstdio>
#include <iostream>

// Wrapper class for _BitInt(N) (port of the C23 feature, compiler
// extension for C++ available in Clang and maybe GCC). This is needed
// because operator<< is not defined on _BitInt(N), apparently.

template<uint64_t N = 128>
class BitInt {
public:
	_BitInt(N) x;

	constexpr BitInt() : x{_BitInt(N)(0)} {}
	constexpr BitInt(int n) : x{_BitInt(N)(n)} {}
	constexpr BitInt(long long n) : x{_BitInt(N)(n)} {}
	constexpr BitInt(_BitInt(N) n) : x{n} {}
	constexpr auto operator<=>(const BitInt& b) const { return x <=> b.x; }
	constexpr bool operator==(const BitInt& b) const = default;
	constexpr BitInt operator+(const BitInt& b) const { return x + b.x; }
	constexpr BitInt operator-(const BitInt& b) const { return x - b.x; }
	constexpr BitInt operator*(const BitInt& b) const { return x * b.x; }
	constexpr BitInt operator/(const BitInt& b) const { return x / b.x; }
	constexpr BitInt operator%(const BitInt& b) const { return x % b.x; }
	constexpr BitInt operator-() const { return -x; }
	constexpr BitInt operator+=(const BitInt& b) { return *this = *this + b; }
	constexpr BitInt operator-=(const BitInt& b) { return *this = *this - b; }
	constexpr BitInt operator*=(const BitInt& b) { return *this = *this * b; }
	constexpr BitInt operator/=(const BitInt& b) { return *this = *this / b; }
	constexpr BitInt operator%=(const BitInt& b) { return *this = *this % b; }

	friend std::ostream& operator<<(std::ostream& os, const BitInt<N>& b) {
		if (b > 0) {
			std::string s;
			auto bb = b;
			while (bb != 0) {
				char c = (bb.x % 10) + '0';
				s = c + s;
				bb /= 10;
			}
			return os << s;
		} else if (b < 0) {
			return os << "-" << -b;
		} else return os << "0";
	}
};

#endif

Generated with cgit - Back to sebastiano.tronto.net