Last active
April 27, 2024 15:58
-
-
Save cgiosy/a441de545c9e96b1d7b02cc7a00561f9 to your computer and use it in GitHub Desktop.
std::bitset addition & subtraction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define private public | |
#include <bitset> | |
#undef private | |
#include <bits/stdc++.h> | |
#include <x86intrin.h> | |
using namespace std; | |
template<size_t _Nw> void _M_do_sub(_Base_bitset<_Nw> &A, const _Base_bitset<_Nw> &B) { | |
for(int i=0, c=0; i<_Nw; i++) | |
c=_subborrow_u64(c, A._M_w[i], B._M_w[i], (unsigned long long*)&A._M_w[i]); | |
} | |
template<> void _M_do_sub(_Base_bitset<1> &A, const _Base_bitset<1> &B) { | |
A._M_w-=B._M_w; | |
} | |
template<size_t _Nb> bitset<_Nb>& operator-=(bitset<_Nb> &A, const bitset<_Nb> &B) { | |
_M_do_sub(A, B); | |
return A; | |
} | |
template<size_t _Nb> inline bitset<_Nb> operator-(const bitset<_Nb> &A, const bitset<_Nb> &B) { | |
bitset<_Nb> C(A); | |
return C-=B; | |
} | |
template<size_t _Nw> void _M_do_add(_Base_bitset<_Nw> &A, const _Base_bitset<_Nw> &B) { | |
for(int i=0, c=0; i<_Nw; i++) | |
c=_addcarry_u64(c, A._M_w[i], B._M_w[i], (unsigned long long*)&A._M_w[i]); | |
} | |
template<> void _M_do_add(_Base_bitset<1> &A, const _Base_bitset<1> &B) { | |
A._M_w+=B._M_w; | |
} | |
template<size_t _Nb> bitset<_Nb>& operator+=(bitset<_Nb> &A, const bitset<_Nb> &B) { | |
_M_do_add(A, B); | |
return A; | |
} | |
template<size_t _Nb> inline bitset<_Nb> operator+(const bitset<_Nb> &A, const bitset<_Nb> &B) { | |
bitset<_Nb> C(A); | |
return C+=B; | |
} | |
int main() { | |
ios::sync_with_stdio(false); cin.tie(nullptr); | |
int a, b; | |
cin >> a >> b; | |
bitset<7> A(a), B(b); | |
cout << (A - B).to_ullong() << endl; | |
bitset<12345> C(a), D(b); | |
cout << (C - D).to_ullong() << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment