X-Git-Url: https://mattmccutchen.net/bigint/bigint.git/blobdiff_plain/3e1327901d299a537a8d932c49dd330f87ac3bda..706f6a7ec4a59f98108a6f3fd8f34fcbfd81f596:/BigUnsigned.cc diff --git a/BigUnsigned.cc b/BigUnsigned.cc index db407c5..9e5a347 100644 --- a/BigUnsigned.cc +++ b/BigUnsigned.cc @@ -625,8 +625,17 @@ void BigUnsigned::bitXor(const BigUnsigned &a, const BigUnsigned &b) { zapLeadingZeros(); } -void BigUnsigned::bitShiftLeft(const BigUnsigned &a, unsigned int b) { +void BigUnsigned::bitShiftLeft(const BigUnsigned &a, int b) { DTRT_ALIASED(this == &a, bitShiftLeft(a, b)); + if (b < 0) { + if (b << 1 == 0) + throw "BigUnsigned::bitShiftLeft: " + "Pathological shift amount not implemented"; + else { + bitShiftRight(a, -b); + return; + } + } Index shiftBlocks = b / N; unsigned int shiftBits = b % N; // + 1: room for high bits nudged left into another block @@ -642,8 +651,17 @@ void BigUnsigned::bitShiftLeft(const BigUnsigned &a, unsigned int b) { len--; } -void BigUnsigned::bitShiftRight(const BigUnsigned &a, unsigned int b) { +void BigUnsigned::bitShiftRight(const BigUnsigned &a, int b) { DTRT_ALIASED(this == &a, bitShiftRight(a, b)); + if (b < 0) { + if (b << 1 == 0) + throw "BigUnsigned::bitShiftRight: " + "Pathological shift amount not implemented"; + else { + bitShiftLeft(a, -b); + return; + } + } // This calculation is wacky, but expressing the shift as a left bit shift // within each block lets us use getShiftedBlock. Index rightShiftBlocks = (b + N - 1) / N;