tags:

views:

1947

answers:

5

As per c99 standard, size of "long long" should be minimum 64 bits. How this is implemented in a 32 bit machine(eg. addition or multiplication of 2 "long long"s).

What is the equivalent of long long in C++.

+1  A: 

If you care about bit-sizes, you should use

#include <stdint.h>

int32_t n;

and friends. This works for C++ as well.

64-bit numbers on 32-bit machines are implemented as you think, by 4 extra bytes. You could therefore implement your own 64-bit datatype by doing something like this:

struct my_64bit_integer {
    uint32_t low;
    uint32_t high;
};

You would of course have to implement mathematical operators yourself.

There is an int64_t in the stdint.h that comes with my GCC version, and in Microsoft Visual C++ you have an __int64 type as well.

csl
+1  A: 

On the IA32 architecture, 64-bit integer are implemented in using two 32-bit registers (eax and edx).

There are platform specific equivalents for C++, and you can use the stdint.h header where available (boost provides you with one).

Edouard A.
+6  A: 

The equivalent in C++ is long long as well. It's not required by the standard, but most compilers support it because it's so usefull.

How is it implemented? Most computer architectures already have built-in support for multi-word additions and subtractions. They don't do 64 bit addititions directly but use the carry flag and a special add-instruction to build a 64 bit add from two 32 bit adds.

The same extension exists for subtraction as well (the carry is called borrow in these cases).

Longword multiplications and divisions can be built from smaller multiplications without the help of carry-flags. Sometimes simply doing the operations bit by bit is faster though.

There are architectures that don't have any flags at all (some DSP chips and simple micros). On these architectures the overflow has to be detected with logic operations. Multi-word arithmetic tend to be slow on these machines.

Nils Pipenbrinck
+1  A: 

As everyone has stated, a 64-bit integer is typically implemented by simply using two 32-bit integers together. Then clever code generation is used to keep track of the carry and/or borrow bits to keep track of overflow, and adjust accordingly.

This of course makes such arithmetic more costly in terms of code space and execution time, than the same code compiled for an architecture with native support for 64-bit operations.

unwind
+1  A: 

The next C++ standard (due 2009, or maybe 2010), is slated to include the "long long" type. As mentioned earlier, it's already in common use.

The implementation is up to the compiler writers, although computers have always supported multiple precision operations. Some languages, like Python and Common Lisp, require support for indefinite-precision integers. Long ago, I wrote 64-bit multiplication and division routines for a computer (the Z80) that could manage 16-bit addition and subtraction, with no hardware multiplication at all.

Probably the easiest way to see how an operation is implemented on your particular compiler is to write a code sample and examine the assembler output, which is available from all the major compilers I've worked with.

David Thornley