Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

long long implementation in 32 bit machine

Tags:

As per c99 standard, size of long long should be minimum 64 bits. How is this implemented in a 32 bit machine (eg. addition or multiplication of 2 long longs). Also, What is the equivalent of long long in C++.

like image 460
chappar Avatar asked Dec 01 '08 09:12

chappar


People also ask

What is the size of long long on your machine?

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. The 32-bit data model for z/OS® XL C/C++ compilers is ILP32 plus long long.

Is 32-bit always long?

long is guaranteed (at least) 32 bits.


2 Answers

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.

like image 58
Nils Pipenbrinck Avatar answered Oct 12 '22 06:10

Nils Pipenbrinck


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).

like image 38
Edouard A. Avatar answered Oct 12 '22 06:10

Edouard A.