File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 3366
    
    9
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: 18103
    
  39

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: int signed or unsigned ?
 
Similar Threads
selecting overloaded method
confusion with primitives
Can anyone pls explain about the output of this prog.
( int a = 2147483648 ) vs (int a = 2147483647 ; a=a+1 )
making signed int to unsigned int ?