Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will a long-integer work on a 32 bit system?

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!

like image 628
user3412636 Avatar asked Mar 12 '14 20:03

user3412636


People also ask

Is long int 32 or 64-bit?

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.

What is the size of an integer on a 32-bit system?

In 32-bit operating systems, the int type is usually 32 bits, or 4 bytes.

How big of a number can 32 bits hold?

The number 2,147,483,647 (or hexadecimal 7FFFFFFF16) is the maximum positive value for a 32-bit signed binary integer in computing.

What data type can hold an 32-bit integer?

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.


2 Answers

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.

like image 84
dcastro Avatar answered Oct 06 '22 03:10

dcastro


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?

like image 39
David Heffernan Avatar answered Oct 06 '22 04:10

David Heffernan