How will you calculate maximum range of "INT" Data Type ?

 Posted by PandianS on 7/9/2010 | Category: Sql Server Interview questions | Views: 8701
Answer:

The Formula Is:
2 ^ (N-1)

here, "N " is nothing but size of the Data type."^ " means Power of the value.

1. We just want to get the Maximum length of the Data type "INT".
Select (max_length * 8) 'Bit(s)' from sys.types Where [name] = 'Int'
The answer is : 32 Bit(s)

2. Now, we can apply the formula for Range
Select Power(Cast(2 as Varchar),(max_length * 8) -1) from sys.types Where [name] = 'Int'
The Result is : 2147483648

The maximum range of "INT" data type is -2147483648 to 2147483647


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Easwaran on: 7/12/2010
The formula is wrong. That should be (2^N)-1.
Posted by: PandianS on: 7/13/2010
hi Easwaran,

What you saying is correct.

But, Please have a look

The Actual size of "INT" data type is 32.

If the Formula is as you saying (2^N)-1 Then the result would be "4294967295". This is WRONG.
ie: (2 ^ 32) -1 = 4294967295 (Wrong)

So the correct one is (2^N-1), Then the result is "2147483648"
ie: (2 ^ 32-1) = (2 ^ 31) = 2147483648

Login to post response