Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

int signed or unsigned ?  RSS feed

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


F:\Binary Literal>javac Int.java
Int.java:5: error: integer number too large: 2147483648
System.out.println(2147483648);
^
1 error
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell me how to represent -12 in binary? And what about -2147483648?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Can you tell me how to represent -12 in binary? And what about -2147483648?



F:\Binary Literal>java Int
-12
 
Henry Wong
author
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:

F:\Binary Literal>javac Int.java
Int.java:5: error: integer number too large: 2147483648
System.out.println(2147483648);
^
1 error



It has always been defined this way. The compiler will range check to make sure that decimal literals fit into the signed integer range.... but for octal and hexidecimal int literals (and now assuming for binary literals too), it just makes sure that it fits into 32 bits. The reason for this is because these types of literals, especially hexidecimal literals (before binary literals) were used to define bit patterns. And it is much easier to define them in an unsigned fashion.

Henry

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!