aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes pls explain? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "pls explain?" Watch "pls explain?" New topic
Author

pls explain?

vikram choudhary .
Greenhorn

Joined: Apr 25, 2007
Posts: 7
byte i= (byte)129;
System.out.println(i);

output: -127 why?
can anyone explain?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
As an int, 129 has the following bit representation

00000000000000000000000010000001

When you cast to a byte, you only have the last 8 bits.

So you have the byte

10000001

Because the first bit is a 1, this is a negative number.

To find out what number it is the negative of, flip all the bits and add 1

You get

01111111

So the number is -127.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Good Keith!!!



Thanks,
cmbhatt


cmbhatt
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: pls explain?
 
Similar Threads
byte range
Casting int to byte (was: pls explain)
simple Bitwise
isn't this strange?
byte range