diff --git a/uint.h b/uint.h index aeb61af..eabfe3d 100644 --- a/uint.h +++ b/uint.h @@ -189,32 +189,37 @@ class UInt { return result; } - template - UInt operator&(const UInt &other) const { - UInt result; - for (int i = 0; i < n_; i++) { - if (i >= other_w) break; + template + UInt operator&(const UInt &other) const { + const int bound_n = cmax(n_, other_n); + UInt 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 - UInt operator|(const UInt &other) const { - UInt result; - for (int i = 0; i < n_; i++) { - if (i >= other_w) break; - result.words_[i] = words_[i] | other.words_[i]; + template + UInt operator|(const UInt &other) const { + const int bound_n = cmax(n_, other_n); + UInt 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 - UInt operator^(const UInt &other) const { - UInt result; - for (int i = 0; i < n_; i++) { - if (i >= other_w) break; - result.words_[i] = words_[i] ^ other.words_[i]; + template + UInt operator^(const UInt &other) const { + const int bound_n = cmax(n_, other_n); + UInt 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; } @@ -347,7 +352,7 @@ class UInt { template UInt<1> operator<=(const UInt &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); @@ -357,7 +362,7 @@ class UInt { template UInt<1> operator>=(const UInt &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); @@ -367,13 +372,13 @@ class UInt { template UInt<1> operator<(const UInt &other) const { - if (n_ < other_w) return UInt<1>(1); + if (w_ < other_w) return UInt<1>(1); return ~(*this >= other); } template UInt<1> operator>(const UInt &other) const { - if (n_ > other_w) return UInt<1>(1); + if (w_ > other_w) return UInt<1>(1); return ~(*this <= other); }