This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes int signed or unsigned ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "int signed or unsigned ?" Watch "int signed or unsigned ?" New topic
Author

int signed or unsigned ?

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583



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


Tell the difficulties that i am difficult.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4101
    
  19

Can you tell me how to represent -12 in binary? And what about -2147483648?


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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

Joined: Sep 28, 2004
Posts: 20169
    
  46

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Thanks Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int signed or unsigned ?
 
It's not a secret anymore!