Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use 'long' instead of 'int' on 64-bits in langs with fixed type size (like Java, C#)

In 10, or even 5 years there will be no [Edit2: server or desktop] 32-bit CPUs.

So, are there any advantages in using int (32bit) over long (64bit) ?
And are there any disadvantages in using int ?


Edit:

  1. By 10 or 5 years I meant on vast majority of places where those langs are used

  2. I meant which type to use by default. This days I won't even bother to think if I should use short as cycle counter, just for(int i.... The same way long counters already win

  3. registers are already 64-bit, there is already no gain in 32 bit types. And I think some loss in 8 bit types (you have to operate on more bits then you're using)

like image 262
Alexander Malakhov Avatar asked Jul 26 '11 03:07

Alexander Malakhov


People also ask

Is long long int 64-bit?

Windows: long and int remain 32-bit in length, and special new data types are defined for 64-bit integers.

Why would you use a type long for a variable instead of type int?

The type long is used where the type int is not that large to hold the desired value. The range of long is –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 which is quite large, to hold the larger values like big whole numbers.

Is Long Long faster than int?

The code could be ran on a 16-bit platform with 32-bit long and 16-bit int on which the int would probably be faster - but not necessarily. On the other hand, on a native 32-bit platform which has 32-bit int and 64-bit long , the long could be faster - but not necessarily.


1 Answers

32-bit is still a completely valid data type; just like we have 16-bit and bytes still around. We didn't throw out 16-bit or 8-bit numbers when we moved to 32-bit processors. A 32-bit number is half the size of a 64-bit integer in terms of storage. If I were modeling a database, and I knew the value couldn't go higher than what a 32-bit integer could store; I would use a 32-bit integer for storage purposes. I'd do the same thing with a 16-bit number as well. A 64-bit number takes more space in memory as well; albeit not anything significant given today's personal laptops can ship with 8 GB of memory.

There is no disadvantage of int other than it's a smaller data type. It's like asking, "Where should I store my sugar? In a sugar bowl, or a silo?" Well, that depends on entirely how much sugar you have.

Processor architecture shouldn't have much to do with what size data type you use. Use what fits. When we have 512-bit processors, we'll still have bytes.

EDIT:

To address some comments / edits..

  1. I'm not sure about "There will be no 32-bit desktop CPUs". ARM is currently 32-bit; and has declared little interest in 64-bit; for now. That doesn't fit too well with "Desktop" in your description; but I also think in 5-10 years the landscape of the type of devices we are writing software will drastically change as well. Tablets can't be ignored; people will want C# and Java apps to run on them, considering Microsoft officially ported Windows 8 to ARM.

  2. If you want to start using long; go ahead. There is no reason not to. If we are only looking at the CPU (ignoring storage size), and making assumptions we are on an x86-64 architecture, then it doesn't make much difference.

  3. Assuming that we are sticking with the x86 architecture; that's true as well. You may end up with a slightly larger stack; depending on whatever framework you are using.

like image 128
vcsjones Avatar answered Oct 13 '22 00:10

vcsjones