kaiseroskilo
kaiseroskilo

Reputation: 1729

64 bit operations

I'm writing code for a primality testing function that handles long long int's.Do I have to use special operators for such large numbers?Is there any documentation concerning large number manipulation in C?I'm using the gnu standard library.Thanks.

Upvotes: 0

Views: 238

Answers (6)

nos
nos

Reputation: 229324

long long is new in C99, though many compilers have supported that as an extension before that.

With gcc a long long is 64 bits, you can use it like any other integer type, nothing special is required.

There's a couple of things to be aware of though, integer constants in the source code needs the LL suffix (or LLU if it's unsigned, e.g. you have to do

long long foo = 123412341234123LL;

and not

long long foo = 123412341234123;

Similarly, for outputting a long long with the printf family, you have to use the conversion specifier "%lld" instead of "%d" or "%ld" (or "%llu" if it's unsigned), e.g.

printf("foo = %lld",foo);

There's some docs about long long in gcc here

Upvotes: 1

Prasoon Saurav
Prasoon Saurav

Reputation: 92924

If you are just handling long long int you don't need anything special as long as your compiler supports it. Take care of overflows while adding and multiplying two long long ints

For handling very large numbers(range much greater than that of long long int) have a look at GNU MP BigNum Library

Upvotes: 0

Jan Hudec
Jan Hudec

Reputation: 76376

If the compiler supports long long int, it works with standard operators.

By the way, long long int is 128 bits on 64-bit unices (where long alone is 64 bits). Use int64_t from <stdint.h> if you need 64-bits on all platforms. This does not apply to 64-bit windows, where long is still 32 bits and long long is 64 bits.

Upvotes: 1

iolo
iolo

Reputation: 1090

If long long ints are supported by your compiler you don't have to do any 'special' kind of stuff. If your processor doesn't support 64-bit types (probably 32-bit-processor then) the compiler will emulate that feature by using sequences of assembly code that breaks up the 64-bit operations into 32-bit ones.

Upvotes: 3

DarkDust
DarkDust

Reputation: 92394

No, you don't need to do anything special. You handle a long long int just the same way as you would handle a int. Just beware of overflows, as with every native integer type.

Upvotes: 3

patapizza
patapizza

Reputation: 2398

Have a look at the GMP library: http://gmplib.org/

Upvotes: -1

Related Questions