If I understand it right an int-variable is saving in 32 bit, restricting it to -2 billion to 2 billion something. However if I use a long-variable it will save in 64 bit allowing a lot more numbers to be stored. I'm sitting on a 64 bit system, but will my code run well on a 32 bit system if I store data in 64 bit?
Thank you!
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.
In 32-bit operating systems, the int type is usually 32 bits, or 4 bytes.
The number 2,147,483,647 (or hexadecimal 7FFFFFFF16) is the maximum positive value for a 32-bit signed binary integer in computing.
Integer, 32 Bit BCD: Unsigned Binary Coded Decimal value ranging from 0 to +99999999. Integer, 32 bit BCD data type is used for numerical tags where variables can only represent in the range from 0-9 within the half-byte boundary.
Don't you worry about that. The long
value will be stored in 2 memory addresses. Int64
/long
will always be 64bit, and Int32
/int
will always be 32bit.
There are a few implications (concerning memory space and performance), but the most noticeable may be that write/read operations won't be atomic on 32bit systems, but you shouldn't expect them to be atomic anyway, since the c# specification makes no such guarantee.
Either way, the point is: this is not something you should ever worry about - the CLR abstracts these things away. Use whichever type suits you best.
On a 32 bit system, operations on a 32 bit integer can be performed in a single machine register. But operations on a 64 bit integer require two machine registers, and are quite a bit less efficient than 32 bit operations. And of course, being twice the size, long
uses more memory than int
. If you have arrays of these types then you will consume your cache twice as fast if you use long
. That can also have performance implications.
So, if you can use int
, you should prefer it to long
. However, if the 32 bits of range afforded by int
are not sufficient for the correctness of your program, you would need to use long
.
Of course, even though operations on 64 bit integers, are less efficient when executed on a 32 bit machine, only profiling would tell you whether or not it actually matters.
Perhaps the bottom line is that programmers should not be wilfully profligate. Why use 64 bits, if 32 bits suffice?
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