开发者

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 man开发者_JS百科ipulation in C?I'm using the gnu standard library.Thanks.


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.


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.


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


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜