Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to represent arbitrarily big numbers in c?

Tags:

c

biginteger

I'm working on a project that requires me to work with numbers larger than the largest numerical datatype in c. I was thinking of using structs with bit fields to represent this, but it's already smelling bad. Anyone got any tips? (Not looking for a library, more of a thought process to go behind doing something like this.)

like image 885
sdellysse Avatar asked Mar 06 '09 17:03

sdellysse


2 Answers

I suggest to first check out the GNU MP Bignum library.

If licensing is a problem you have to roll your own. My first choice for the data-type would be a simple array of unsigned chars along with some extra data to denote how large that array is.

Something like this:

typedef struct 
{
  unsigned char * NumberData;
  size_t          AllocatedSize;
} MyBigNum;

Should be sufficient.

like image 41
Nils Pipenbrinck Avatar answered Dec 09 '22 00:12

Nils Pipenbrinck


The GNU MP Bignum Library would be my first choice.

like image 168
f3lix Avatar answered Dec 08 '22 22:12

f3lix