diff options
-rw-r--r-- | number.cpp | 35 | ||||
-rw-r--r-- | number.h | 15 |
2 files changed, 50 insertions, 0 deletions
@@ -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; @@ -49,6 +49,14 @@ public: number operator*(const number &) const; /** + * \brief Return a number equals to this number where n bits have been + * shifted to the left. + * + * \param n Number of bits to shift. + */ + number operator<<(std::uint32_t n) const; + + /** * \brief Tells whether the number passed as parameter is strictly less * than this number. * @@ -104,6 +112,13 @@ public: number &operator+=(const number &n); /** + * \brief Shift the number n bits to the left. + * + * \param n Number of bits to shift. + */ + number &operator<<=(std::uint32_t); + + /** * \brief Increment this number. */ number &operator++(); |