C ++ arbitrary integer lengths

In C ++ can I define arbitrary integers?

So instead of using uint64_t for something between 33 and 64 bits, I could define my own 34-bit, 36-bit, etc. whole numbers.

+6
source share
3 answers

The compiler has its own types, as you mentioned. (32 bits on most platforms) and long (64 bits on most platforms). If you need support for large integers, you can use different libraries that limit the size of an integer to the size of your memory.

Libraries:

+1
source

Since you live in a C ++ world, use https://gmplib.org/

Gotta do the trick

+1
source

For computation, this will not give you advantages, because today processors are optimized for 32-bit or 64-bit arithmetic.

If you need them for size problems, it might make sense to define your own container of n-bit numbers, and it can be easily encoded.

Even more general may be a container for mod-n numbers (i.e. for numbers from 0 to n-1, not necessarily modulo with an exact power of two). For this, a simple solution (but not optimal in space) could be based on the greatest power n, which corresponds to a 64-bit integer (for example, you can pack 22 numbers between 0 and 6 into one number from 0 to 2 ** 64 -1 )

+1
source

All Articles