bueddl

Untitled

Feb 1st, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. extern "C" void _add(unsigned int[], const unsigned int[], int);
  5.  
  6. template<int NBits>
  7. class BigInt
  8. {
  9.     static constexpr int m_block_count = NBits >> 5;
  10.     unsigned int m_block[m_block_count];
  11.  
  12. public:
  13.     BigInt& operator=(const int imm)
  14.     {
  15.         m_block[0] = imm;
  16.         for (int i = 1; i < m_block_count; ++i)
  17.             m_block[i] = 0;
  18.         return *this;
  19.     }
  20.  
  21.     BigInt& operator+=(const BigInt &rhs)
  22.     {
  23.         _add(m_block, rhs.m_block, m_block_count);
  24.         return *this;
  25.     }
  26.  
  27.     BigInt operator+(const BigInt &rhs)
  28.     {
  29.         BigInt result = *this;
  30.         result += rhs;
  31.         return result;
  32.     }
  33.  
  34.     std::ostream& operator<<(std::ostream &stream) const
  35.     {
  36.         for (int i = 0; i < m_block_count; ++i)
  37.             stream << std::hex << std::setw(8) << std::setfill('0') << m_block_count;
  38.         return stream;
  39.     }
  40.  
  41. private:
  42.     friend
  43.     std::ostream& operator<<(std::ostream &stream, const BigInt<NBits> &rhs)
  44.     {
  45.         for (int i = rhs.m_block_count - 1; i >= 0 ; --i)
  46.             stream << std::hex << std::setw(8) << std::setfill('0') << rhs.m_block[i];
  47.         return stream;
  48.     }
  49. };
  50.  
  51. int main()
  52. {
  53.     BigInt<1024> a, b;
  54.  
  55.     a = 0xFFFFFFFF;
  56.     b = 0xFFFFFFFF;
  57.  
  58.     for (int i = 0; i < 10000; ++i) {
  59.         a += b;
  60.         std::cout << a << std::endl;
  61.     }
  62.  
  63.     return 0;
  64. }
Add Comment
Please, Sign In to add comment