summaryrefslogtreecommitdiff
path: root/number.cpp
diff options
context:
space:
mode:
authorOlivier Gayot <og@satcom1.com>2017-11-19 11:51:41 +0100
committerOlivier Gayot <og@satcom1.com>2017-11-19 16:21:04 +0100
commit5a2d18587b53133bc831967ccd8d6a5dd6e54190 (patch)
treeae31c221b8b86dab255755317132caef2fc38495 /number.cpp
parent6843dc1272e56e91234deab486781c711436031e (diff)
Added the left-shift computation
Signed-off-by: Olivier Gayot <og@satcom1.com>
Diffstat (limited to 'number.cpp')
-rw-r--r--number.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/number.cpp b/number.cpp
index d16f37f..497cf64 100644
--- a/number.cpp
+++ b/number.cpp
@@ -106,6 +106,33 @@ number::operator*(const number &n) const
return result;
}
+number
+number::operator<<(std::uint32_t shift) const
+{
+ if (shift > 32) {
+ /* TODO */
+ throw std::out_of_range("> 32");
+ }
+
+ number result(*this);
+
+ std::uint32_t carry = 0;
+
+ for (auto &operand: result._operands) {
+ std::uint32_t tmp = ((1ull * operand) << shift) + carry;
+
+ carry = ((1ull * operand) >> (32 - shift));
+
+ operand = tmp;
+ }
+
+ if (carry) {
+ result._operands.push_back(carry);
+ }
+
+ return result;
+}
+
/* }}} */
/* Comparison operators {{{ */
@@ -174,6 +201,14 @@ number::operator+=(const number &n)
}
number &
+number::operator<<=(std::uint32_t n)
+{
+ *this = *this << n;
+
+ return *this;
+}
+
+number &
number::operator++()
{
*this = *this + 1;