• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int's range is a big confusion...!

 
golu jain
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks!

I have a basic question:-
1.>int i=2147483648;
The above code will generate the compile time error for the value being one more than the positive range.But....
2.>int i=-(-2147483648);
The above code will compile successfully.and the output will be
'-2147483648'.
Why does this things compiles b'coz after negating those minus signs the value is still out of int's range.
Help me
Golu Jain
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3714
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is the range of signed types is not symetric. For. eg. int's range is -2^31 to 2^31-1. You can see that, it can store 1 negative number more than positive no.
Now, at runtime when you negate the largest -ive no., it's positive value falls outside the range by 1. So, in effect you get the same negative number.
HTH,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic