Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Biginteger, what does this mean?

Tags:

c++

I am puzzled about why we need a BASE=100000000 and WIDTH=8. This code is from a book, but I don't understand it.

struct Biginteger {
  static const BASE = 100000000;
  static const WIDTH = 8;

  vector<int> s;
  Biginteger(long long int num = 0) { *this = num; }
  Biginteger operator=(long long num) {
    s.clear();
    do {
      s.push_back(num % BASE);
      num /= BASE;
    } while (mun > 0);
    return *this;
  }

1 Answers

Primitive types is not able to store arbitrarily large numbers and this is why you need BigInteger here. The idea of BigInteger is to use sequence of small numbers in primitive types to represent a large number.

With BASE=100000000 and WIDTH=8, you are actually separating the original number 8 digits by 8 digits.

For example 254325623456546 will be (2543256, 23456546)

You can simply change to other (BASE, WIDTH). For example, BASE=10, WIDTH=1 means you are storing the number digit by digit. For instance, 254325623456546 will be (2, 5, 4, 3, 2, 5, 6, 2, 3, 4, 5, 6, 5, 4, 6).

like image 145
Jerry Chou Avatar answered Mar 22 '26 19:03

Jerry Chou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!