Integers larger than 64bit - Arithmetic

M

Martin Johansen

Hi.

Is it possible to use the C-library to handle integers arbitrary large?

I know how to do arbitrary arithmetic to add and convert the base of a
number, but is there a way to utilize the c-library to aid this?

For example, printf can print an 64-bit integer to base 10, but can you
convert a 512-bit integer the same fashion somehow?

Thanks!
 
M

Malcolm

Martin Johansen said:
Is it possible to use the C-library to handle integers arbitrary large?
You have to write your own code. For example

typedef struct
{
unsigned char *data;
int len;
int base;
} HUGE;

Then a function

char *hugetodecimal(HUGE *x)

You also need functions for the basic arithemtic operations.

There are also several huge integer libraries available on the net, e.g. one
at snippets.org.
 
C

Christian Bau

"Martin Johansen said:
Hi.

Is it possible to use the C-library to handle integers arbitrary large?

I know how to do arbitrary arithmetic to add and convert the base of a
number, but is there a way to utilize the c-library to aid this?

For example, printf can print an 64-bit integer to base 10, but can you
convert a 512-bit integer the same fashion somehow?

printf cannot do this.

You may find a library which can, or you can quite easily write that
code yourself. If the intention is to convert the number into something
that is readable by a human, then you have to give it some more thought.
 
K

Karthik

Martin said:
Hi.

Is it possible to use the C-library to handle integers arbitrary large?

I know how to do arbitrary arithmetic to add and convert the base of a
number, but is there a way to utilize the c-library to aid this?

For example, printf can print an 64-bit integer to base 10, but can you
convert a 512-bit integer the same fashion somehow?

Thanks!

Try implementing a linked list.

typedef struct node {
long val;
struct node * next;
} LongNum;

This way you can store very long values. And you can define
appropriate functions like add, subtract, print etc on the linked list.

HTH
 
S

Stephen Sprunk

Martin Johansen said:
Is it possible to use the C-library to handle integers arbitrary large?

I know how to do arbitrary arithmetic to add and convert the base of a
number, but is there a way to utilize the c-library to aid this?

For example, printf can print an 64-bit integer to base 10, but can you
convert a 512-bit integer the same fashion somehow?

The standard C library does not, but there are many third-party libraries
(e.g. OpenSSL) that contain a "bignum" implementation.

S
 
D

Dan Pop

In said:
Is it possible to use the C-library to handle integers arbitrary large?

No, the C-library only supports fixed-size integers.
I know how to do arbitrary arithmetic to add and convert the base of a
number, but is there a way to utilize the c-library to aid this?
Nope.

For example, printf can print an 64-bit integer to base 10, but can you
convert a 512-bit integer the same fashion somehow?

Nope, unless your implementation supports something like int512_t, again,
as a fixed-size integer type.

For arbitrarily sized integers you must use external libraries, the
standard C library provides no support for them.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top