Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long long int on 32 bit machines

Tags:

c++

c

types

very simple question, I read that GCC supports long long int type. But how can make math operations with it, when CPU is 32 bit wide only?

like image 797
B.Gen.Jack.O.Neill Avatar asked Jun 18 '10 19:06

B.Gen.Jack.O.Neill


People also ask

What is the size of int in 32-bit machine?

In 32-bit operating systems, the int type is usually 32 bits, or 4 bytes.

How big is a long on a 32-bit system?

long has a minimum size of 32 bits, that's it.

Is long int 32 or 64-bit?

int , long , ptr , and off_t are all 32 bits (4 bytes) in size. int is 32 bits in size. long , ptr , and off_t are all 64 bits (8 bytes) in size.

How many bits is a long long int?

It takes a size of 64 bits. A maximum integer value that can be stored in an unsigned long long int data type is 18, 446, 744, 073, 709, 551, 615, around 264 – 1(but is compiler dependent).


1 Answers

The compiler will synthesize math operations (or use function calls) that use more than one CPU instruction to perform the operation. For example, an add operation will add the low order components (the low words) of the long long values and will then take the carry out of that operation and feed it into an add operation on the high order words of the long long.

So the following C code:

long long a;
long long b;
long long c;

// ...
c = a + b;

might be represented by an instruction sequence that looks something like:

mov eax, [a.low]   ; add the low order words
add eax, [b.low]

mov edx, [a.high]  ; add the high order words, 
adc edx, [b.high]  ; including the carry 

mov [c.low], eax
mov [c.high], edx

And if you consider for a moment, compilers for 8 and 16 bits systems had to do this type of thing for 16 and/or 32-bit values long before long long came into being.

like image 177
Michael Burr Avatar answered Oct 08 '22 04:10

Michael Burr