summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Gayot <og@satcom1.com>2017-11-20 09:57:56 +0100
committerOlivier Gayot <og@satcom1.com>2017-11-20 09:59:47 +0100
commitef6b1c0dd80a7b37161557aef49d01724f7f7531 (patch)
tree0f867f5c2091867bbe07b54c431a4ee289271469
parent69532f5e0383db0f0e71f3db585177660d8ef948 (diff)
Allow left-shifts of more than 32 bits
Signed-off-by: Olivier Gayot <og@satcom1.com>
-rw-r--r--number.cpp15
1 files changed, 12 insertions, 3 deletions
diff --git a/number.cpp b/number.cpp
index 94ef1ee..c3dd33c 100644
--- a/number.cpp
+++ b/number.cpp
@@ -151,13 +151,22 @@ number::operator*(const number &n) const
number
number::operator<<(std::uint32_t shift) const
{
- if (shift > 32) {
- /* TODO */
- throw std::out_of_range("> 32");
+ if (*this == 0) {
+ return 0;
}
number result(*this);
+ /*
+ * Since, internally, we store 32-bits-wide integers, we can just insert
+ * "shift / 32" new integers at the beginning.
+ */
+ for (unsigned int i = 0; i < shift / 32; ++i) {
+ result._operands.push_front(0);
+ }
+
+ shift %= 32;
+
std::uint32_t carry = 0;
for (auto &operand: result._operands) {