Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modified and/or/xor to work with zeros. also fixed issue using n_ ins… #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions uint.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,32 +189,37 @@ class UInt {
return result;
}

template<int other_w>
UInt<w_> operator&(const UInt<other_w> &other) const {
UInt<w_> result;
for (int i = 0; i < n_; i++) {
if (i >= other_w) break;
template<int other_w, class other_t, int other_n>
UInt<cmax(w_,other_w)> operator&(const UInt<other_w, other_t, other_n> &other) const {
const int bound_n = cmax(n_, other_n);
UInt<cmax(w_,other_w)> result;
for (int i = 0; i < bound_n; i++) {
if (i >= other_w || i >= w_) break;
result.words_[i] = words_[i] & other.words_[i];
}
return result;
}

template<int other_w>
UInt<w_> operator|(const UInt<other_w> &other) const {
UInt<w_> result;
for (int i = 0; i < n_; i++) {
if (i >= other_w) break;
result.words_[i] = words_[i] | other.words_[i];
template<int other_w, class other_t, int other_n>
UInt<cmax(w_, other_w)> operator|(const UInt<other_w, other_t, other_n> &other) const {
const int bound_n = cmax(n_, other_n);
UInt<cmax(w_, other_w)> result;
for (int i = 0; i < bound_n; i++) {
if (i < other_w && i < w_) result.words_[i] = words_[i] | other.words_[i];
else if (i >= other_w) result.words_[i] = words_[i];
else result.words_[i] = other.words_[i];
}
return result;
}

template<int other_w>
UInt<w_> operator^(const UInt<other_w> &other) const {
UInt<w_> result;
for (int i = 0; i < n_; i++) {
if (i >= other_w) break;
result.words_[i] = words_[i] ^ other.words_[i];
template<int other_w, class other_t, int other_n>
UInt<cmax(w_,other_w)> operator^(const UInt<other_w, other_t, other_n> &other) const {
const int bound_n = cmax(n_, other_n);
UInt<cmax(w_, other_w)> result;
for (int i = 0; i < bound_n; i++) {
if (i < other_w && i < w_) result.words_[i] = words_[i] ^ other.words_[i];
else if (i >= other_w) result.words_[i] = !words_[i];
else result.words_[i] = !other.words_[i];
}
return result;
}
Expand Down Expand Up @@ -347,7 +352,7 @@ class UInt {

template<int other_w>
UInt<1> operator<=(const UInt<other_w> &other) const {
// if (n_ < other_w) return UInt<1>(1); //test faled when included. not sure why
// if (n_ < other_w) return UInt<1>(1); //test fail when included. not sure why
for (int i=n_-1; i >= 0; i--) {
if (words_[i] < other.words_[i]) return UInt<1>(1);
if (words_[i] > other.words_[i]) return UInt<1>(0);
Expand All @@ -357,7 +362,7 @@ class UInt {

template<int other_w>
UInt<1> operator>=(const UInt<other_w> &other) const {
if (n_ > other_w) return UInt<1>(1);
if (w_ > other_w) return UInt<1>(1);
for (int i=n_-1; i >= 0; i--) {
if (words_[i] > other.words_[i]) return UInt<1>(1);
if (words_[i] < other.words_[i]) return UInt<1>(0);
Expand All @@ -367,13 +372,13 @@ class UInt {

template<int other_w>
UInt<1> operator<(const UInt<other_w> &other) const {
if (n_ < other_w) return UInt<1>(1);
if (w_ < other_w) return UInt<1>(1);
return ~(*this >= other);
}

template<int other_w>
UInt<1> operator>(const UInt<other_w> &other) const {
if (n_ > other_w) return UInt<1>(1);
if (w_ > other_w) return UInt<1>(1);
return ~(*this <= other);
}

Expand Down