0

For example, if I add a short integer and a float data type, will each allocate the same amount of memory? They have different maximum values, but have different data structures as well, so I was not sure. Also, do mathematical operations take different amounts of time with different data types?

  • What language/platform do you use? Adding appropriate tags will help you to have the best answers. – Artemix Sep 19 '12 at 07:24

2 Answers2

2

It's hard to answer this exactly without knowing which language/hardware you are using, but in most cases different data types do take up different amounts of memory and data type can affect the speed of mathematical operations. For example, in Java and C#, a short is 16 bits and a float is 32 bits. I would expect floating point math to be slower than integer math in general, although this can be complex as explained in this post.

For other languages (such as C), the size of the basic data types is hardware-dependent, so an int might be 32 bits on some machines and 64 bits on others.

Community
  • 1
  • 1
ChaseMedallion
  • 19,262
  • 13
  • 76
  • 137
1

the reason we have datatypes is efficiency. That includes both size and treatment of data(types).

A char datatype has 8 bits (you may also call it 8-bit integer since the numerical values are simply mapped to characters in the ASCII table) = 256 possible values

A 32-bit integer has ... you guessed it 32 bits = 2³² possible values

Mathematical Operations are performed by shifting or comparing those bits..

More bits = more operations = more time

Reference: http://en.wikipedia.org/wiki/Data_type

Gung Foo
  • 12,626
  • 5
  • 29
  • 39