Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. BigNumber::BigNumber(std::string input) { //Remember to check if all literals are not digits!
  2.     uint32_t input_len = input.size();
  3.  
  4.     if (input_len == 0) {
  5.         throw "String used for class instance construction must not be empty.";
  6.     }
  7.  
  8.     if (input_len == 1 && input[0] == 0) {
  9.         this->size = 0;
  10.         return;
  11.     }
  12.  
  13.     uint32_t current_alloc_size = BigNumber::ALLOC_SIZE;
  14.     uint32_t start, len, current_bit;
  15.  
  16.     start = this->is_negative = input[0] == '-';
  17.     len = current_bit = 0;
  18.  
  19.     this->blocks.push_back(*create_new_block(current_alloc_size)); //Here we create and add empty block to the list.
  20.     Block *current_block = &this->blocks.back(); //Points to created element on the list.
  21.  
  22.     int64_t *current_elements = current_block->get_bits_array();
  23.     int64_t current_element = 0;
  24.     uint32_t BITS_PER_ELEMENT = sizeof(uint64_t) * Architecture::PlatformChecks::bits_per_byte(); // should move to header
  25.     while (start < input_len - 1 || input[input_len - 1] != '0') {
  26.         if (current_bit == BITS_PER_ELEMENT) {
  27.             current_bit = 0;
  28.             ++current_element;
  29.             ++len;
  30.             if (current_element == current_alloc_size) {
  31.                 current_alloc_size = current_alloc_size << 1;
  32.                
  33.                 this->blocks.push_back(*create_new_block(current_alloc_size)); //Here we create and add empty block to the list.
  34.                 Block *current_block = &this->blocks.back(); //Points to created element on the list.
  35.  
  36.                 current_elements = current_block->get_bits_array();
  37.                 current_element = 0;
  38.             }
  39.         }
  40.  
  41.         current_elements[current_element] += ((input[input_len - 1] & 1) << current_bit);
  42.         int tmp = ((input[input_len - 1] & 1) << current_bit); // for debug purposes
  43.         for (int32_t i = start; i < input_len; ++i) {
  44.             if (i < input_len - 1)
  45.                 input[i + 1] += 10 * (input[i] & 1);
  46.             input[i] = (input[i] - '0' >> 1) + '0';
  47.         }
  48.         if (input[start] == '0')
  49.             ++start;
  50.  
  51.         ++current_bit;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement