This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int i =2147483647 //(Max_Value);int j=i*2; j=?

 
sam huang
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result is -2.
Why?
[ January 04, 2003: Message edited by: sam huang ]
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In binary,
i = 0111 1111 1111 1111 1111 1111 1111 1111 (you can verify, this add up to 2147483647)
j = i * 2
= 1111 1111 1111 1111 1111 1111 1111 1110
First bite is 1, so j is negative.
| j | = 0000 0000 0000 0000 0000 0000 0000 0010
so j = -2
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic