0

I was working on a windows 7 32bit computer, I thought 32bit OS will not be able to allocate an 64bit unsigned number. but I tried any way... in a C++ program I wrote the following code..

#include <iostream>
#include <climits>
using namespace std;

int main() {
    unsigned long long i = 0;

    cout << i << endl;

    return 0;
}

though I was expecting an error the program compiled and ran successfully! But I don't understand how! can anyone please explain it to me... thanks in advance :)

Zishan Onik
  • 59
  • 1
  • 10
  • Yes it can. But how do you know that on your platform `unsigned long long` is 64 bits? – Richard Critten Jan 30 '16 at 13:07
  • actually I assigned the value of ULLONG_MAX to variable i then printed it.. and got.. 18446744073709551615 which is you know 2^64 - 1 thats why the question came into my mind.. – Zishan Onik Jan 30 '16 at 13:20
  • @RichardCritten With a standard-compliant C++ compiler, "if" a type `unsigned long long` exists, it must be able to hold at least values from 0 to 2^64-1, so it can't be less than 64bit. (but again, "if" it exists, it doesn't have to. If not, this code won't compile) – deviantfan Jan 30 '16 at 13:26
  • @RichardCritten - with a standard-compliant C++ compiler, `unsigned long long` must exist. It's a standard type. – Pete Becker Jan 30 '16 at 14:15
  • @RichardCritten - I'm confused. `unsigned long long` is a standard type, and, as you said, it must at least be able to hold all values in the range [0, 2^64). So the expectation that it would be (at least) 64 bits is correct. – Pete Becker Jan 30 '16 at 17:15
  • Why can't it? The computer can do arithmetics in as much precision as possible as using [Arbitrary-precision arithmetic](https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic), given enough memory (and time). Just like humans can do 1000-digit (or more) arithmetics given enough time and "paper". [Do I need to have 64 bit Processor to use 64 bit data type](http://stackoverflow.com/q/5530906/995714) – phuclv Jul 31 '16 at 15:26

2 Answers2

2

Let's see what it compiles to. I've added some arithmetic to make it clearer.

6           unsigned long long i = 0;
   0x0804868d <+18>:    mov    DWORD PTR [ebp-0x10],0x0
   0x08048694 <+25>:    mov    DWORD PTR [ebp-0xc],0x0

7           unsigned long long j = 3;
   0x0804869b <+32>:    mov    DWORD PTR [ebp-0x18],0x3
   0x080486a2 <+39>:    mov    DWORD PTR [ebp-0x14],0x0

8           cout << i + j << endl;
   0x080486a9 <+46>:    mov    ecx,DWORD PTR [ebp-0x10]
   0x080486ac <+49>:    mov    ebx,DWORD PTR [ebp-0xc]
   0x080486af <+52>:    mov    eax,DWORD PTR [ebp-0x18]
   0x080486b2 <+55>:    mov    edx,DWORD PTR [ebp-0x14]
   0x080486b5 <+58>:    add    eax,ecx
   0x080486b7 <+60>:    adc    edx,ebx

As you can see, the compiler generates two instructions for load and store. Addition is performed using adc (add with carry) to carry the carry bit to the higher part.

iter
  • 398
  • 2
  • 4
  • ok so 2 32bit registers are being used to store a 64bit number. so is there any way to store 128 bit number in computer that has 64bit processor and 64bit OS ?? if so can you tell me how to do it in C/C++ ? – Zishan Onik Jan 30 '16 at 13:46
  • @ZishanOnik There might be, depending on your compiler. Search for `uint128_t` – iter Jan 30 '16 at 13:47
  • I'm using GCC Code::Blocks.. I know about some big integer library... but what I really want is RAX:RBX kinda thing... Thanks a lot for the help though :D – Zishan Onik Jan 30 '16 at 13:51
1

A 64-bit CPU can load a 64-bit unsigned number directly into a register, whereas on a 32-bit CPU it has to work on parts of the number at a time. The C++ compiler hides these differences from you. We had the ability to process 64-bit unsigned numbers on 8-bit machines, it just took many more cycles to do it.

  • okay... so 32bit or 64bit OS has nothing to do with it? and I guess you are talking about assigning the 64bit value into two different registers for eample EAX:EBX and compute that way or creating an array of values like BigInteger library of java? if the answer is EAX:EBX register thing... so is there anyway I can compute 128bit value in 64bit machine by putting it in RAX:RBX?? if there is would you explain how ? thanks :D – Zishan Onik Jan 30 '16 at 13:30