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.
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.
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.
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.
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.
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 int
s 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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With