Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to use 64bit integers in a 32bit application?

Tags:

c++

c

int

int64

I notice in C and C++, we can use int64_t, or simply a long long.

If I compile 32bit code using these types, will I suffer any performance issues on 64bit and/or 32bit machines?

Aside from saving some RAM, would I ever have a reason to just use int?
After all, 64bit ints are far more useful in storing large numbers.

like image 987
Nyaarium Avatar asked Mar 11 '15 02:03

Nyaarium


People also ask

Can a 64-bit system run a 32bit program?

Can I run 32-bit programs on a 64-bit computer? Most programs made for the 32-bit version of Windows will work on the 64-bit version of Windows except for most Antivirus programs. Device drivers that are made for the 32-bit version of Windows will not work correctly on a computer running a 64-bit version of Windows.

What is the difference between 32-bit and 64-bit integer?

A 32 bit Signed Integer can house a number from −2,147,483,648 to 2,147,483,647 Unsigned: 0 to 4,294,967,295. A 64 bit Signed Integer can house a number from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Unsigned: 0 to 18,446,744,073,709,551,615.

Why do people go from 32bit to 64bit?

Here's why it matters Simply put, a 64-bit processor is more capable than a 32-bit processor because it can handle more data at once. A 64-bit processor can store more computational values, including memory addresses, which means it can access over 4 billion times the physical memory of a 32-bit processor.

Is Java int 32 or 64-bit?

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1.


2 Answers

If I compile 32bit code using these types, will I suffer any performance issues on 64bit and/or 32bit machines?

Your compiler may need to generate several machine code instructions to perform operations on the 64 bit values, slowing down those operations by several times. If that might be a concern, you'd want to do some benchmarking to assess the impact on a particular program with realistic data. That issue exists where you're executing the 32 bit executable on a 32 or 64 bit machine.

would I ever have a reason to just use int?

Aside from performance and memory usage, there's occasionally reason to use ints because other APIs/streams etc. that you work with use int. There's also subtle documentary value in using int if it's clearly adequate, otherwise other programmers may waste time wondering why you'd gone out of your way to use a long long.

After all, 64bit ints are far more useful in storing large numbers.

Far more useful in storing very large numbers - sure - but that's relatively rarely needed. If you're storing something like a year or someone's age, there's just no particular point in having 64 bits.

like image 62
Tony Delroy Avatar answered Oct 18 '22 21:10

Tony Delroy


If I compile 32bit code using these types, will I suffer any performance issues on 64bit and/or 32bit machines?

64 bit integers on 32 bit architectures require two registers to store the value*. 32 bit values require only one register. This matters because:

  • You may run out of registers sooner, requiring registers to be spilled to memory. This takes more time.
  • Loading and storing 2 registers typically takes two instructions, not one.
  • Doing addition or subtraction on operands in two registers takes two or more instructions versus just one for operands in just one register.

Bottom line is that if 32 bit performance is important, don't use 64 bit integers unless you need them.s

* This is true for x86, ARM, and PowerPC processors, which covers most of the processors people program for these days. It is probably true of most other processors as well.

like image 38
Craig S. Anderson Avatar answered Oct 18 '22 20:10

Craig S. Anderson