aspose file tools*
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
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: 3649
    
  17

Can you tell me how to represent -12 in binary? And what about -2147483648?
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: 19070
    
  40

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 ?