From 6433343535687b4ab2e873f2410a7bd0d048e323 Mon Sep 17 00:00:00 2001 From: floofyjin Date: Sat, 20 May 2023 16:16:43 -0700 Subject: [PATCH] modified and/or/xor to work with zeros. also fixed issue using n_ instead of w_ for comparisons --- uint.h | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) 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); }