File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes byte casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "byte casting" Watch "byte casting" New topic
Author

byte casting

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
How is it that System.out.println((byte)128)
gives -128 as output?
128 is beyond the range of byte, so calcualtion will proceed as 128%256.
But what about the -ve sign?
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75
byte 8 bit
max value it can hold " 0 1 1 1 1 1 1 1 "

8th bit is sign bit & value is +127

128 default data type is int (32 bytes)
128 ==> "0 0 0 0 0 0 0 0 - 0 0 0 0 0 0 0 0 - 0 0 0 0 0 0 0 0 - 1 0 0 0 0 0 0 0"

cast it(32 bits) to byte(8 bits) i.e pick 8 bits from right side i.e
1 0 0 0 0 0 0 0

Please convert this value to decimal value.


I am not in touch with this (shift operator & conversions), pls. wait for better response.
[ November 27, 2004: Message edited by: sanjeevmehra mehra ]

thanks & regards,<br />Sanjeev.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: byte casting