Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of 64 bit over 32 bit when writing software [duplicate]

If I have a simple program written in C++ like HelloWorld, and then I compile it in a machine of 32 bit and 64 bit, I get two different binaries doing the same but they are different machine code and only 32 bit binary will be able to run on 32 or 64 bit machine.

In both case I don't have any benefits because the source code is the same and they do the same. This makes me to think the all software packages of some Linux distro written for 32 bit could be ported to 64 bit machine without to do anything change. Then, what do I get ? Any benefits?

Is there any example of code in C/C++ that I can do some in 64-bit that I can't do in 32-bit ?

For example, Google Chrome right now is unsupported in 32 bit, but not in 64 bit. Which could be the reason?

like image 421
rvillablanca Avatar asked Feb 07 '23 11:02

rvillablanca


2 Answers

There are too many differences (memory handling, CPU architecture, bus, etc.) between a 32-bit and 64-bit CPU to get into here, but the biggest and most obvious difference is addressable memory (i.e. how far your pointer can go).

Take the following code for example:

#include <iostream>

int main(int argc, char* argv[])
{
    // this is just to demonstrate 32 vs. 64
    int* x = (int*)0xFFFFFFFFFFFFFFFF;
    int* y = (int*)0x00000000FFFFFFFF;
    std::cout << std::hex << 
        "&x = " << x << std::endl <<
        "&y = " << y << std::endl;
    if (y == x) {
        std::cout << "RIGHT!" << std::endl;
    } else {
        std::cout << "WRONG!" << std::endl;
    }
    return 0;
}

Q: What do you think will be printed on a 32-bit machine vs. a 64-bit machine?

A: A very different result!

As you can see from the above code, if I expect x to equal y and test this on a 32-bit machine, then things will go as I expect and my code will run fine and everyone's happy! But then I pass this code to a friend who has to recompile for their 64-bit machine and they are most certainly not happy since all they see is WRONG!

I won't go deep into the other differences of 32 vs. 64 (like device and system drivers, or kernel modules) since it's beyond the scope of this forum, but hopefully the above code can illustrate why building for a 32-bit machine and then re-compiling for a 64-bit machine isn't as cut and dry as one would initially think.

So to answer some of your questions more directly:

Then, what do I get ? Any benefits?

It depends on what you're trying to do. If you have a program that will never reach the limits of 32-bit CPU's then you won't necessarily see any benefits of building for a 64-bit CPU, and depending on the CPU and OS, you might actually see a degradation in performance (as was the case in the early days of 32-bit emulation on 64-bit CPU's), but with modern cores and OS's, this is largely a non-issue for the "average" program (save the fact that you can't access more than 4GB of RAM).

However, if you have a project that would consume massive amounts of memory (like a web-browser), or need to do calculations for very large sets of numbers (like 3D calculations), then you will most certainly see a benefit in the fact that you can address more than 4GB of RAM or larger resolution numbers for your 64-bit build.

It just depends on the scope of your project and what architectures you're willing to support.

For example, Google Chrome right now is unsupported in 32 bit, but not in 64 bit. Which could be the reason?

Only the Chrome team can specifically tell you why to this one, but my guess has to do with a couple of reasons.

First is the fact that 32-bit CPU's are largely dying out and thus killing off support for a dying architecture means they can focus on improving the 64-bit architecture.

The second reason probably has to do with memory; the 64-bit version of Chrome can access more than 4GB of RAM (assuming the system has more than that) and thus a 64-bit machine with 8GB of RAM would be able to handle more browser sessions and potentially be more responsive (to the individual sessions) than on a 32-bit machine.

Additionally, Wiki has a pretty good page that details more of the 32-bit to 64-bit transition and the various considerations, should you be interested in diving in more on the differences.

Hope that can help.

like image 151
txtechhelp Avatar answered Feb 10 '23 00:02

txtechhelp


64-bit calculations can be faster than 32-bit on x64 platforms. 64-bit program can also can use more RAM (not limited by 4 Gb).

like image 23
Andrei R. Avatar answered Feb 10 '23 00:02

Andrei R.