-3

So, is there really any advantage to using int16? I can see how you might use it if you know your number is never going to use more than 16 bits. However, the compiler (as I understand it) optimizes for int (aka "int32") anyway. Also, it seems more common practice to use int, in the first place.

So, why would one use int16?

sbman12
  • 67
  • 1
  • 2
  • 6

2 Answers2

5

There are mainly two reasons that you might want to use Int16 rather than Int32:

  • You have a large array of them, and want to save some memory.

  • You are interfacing with something else, that expects an Int16.

In normal code there is no good reason to prefer Int16. Using an Int32 for example as a loop counter is faster, both in x86 mode and x64 mode. (An Int32 is also faster than an Int64 even in x64 mode.)

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

You use an Int16 over Int32, when the numbers you are going to represent and make calculations with them etc. can be represented as Int16. In other words all depends on your data.

For instance, I have seen many people represent a 10 digit phone number as an int, which is an Int32. Usually we don't make any calculations with a phone number. Conceptually, it would be better if they had used a string for this purpose, or a char[10] instead of using an int. However, if they had done so, they would had seen any significant change.

Christos
  • 53,228
  • 8
  • 76
  • 108