This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets 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: 3581
    
  14

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: 18509
    
  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
 
Consider Paul's rocket mass heater.
 
subject: int signed or unsigned ?
 
Similar Threads
making signed int to unsigned int ?
( int a = 2147483648 ) vs (int a = 2147483647 ; a=a+1 )
selecting overloaded method
confusion with primitives
Can anyone pls explain about the output of this prog.