It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes pls explain? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "pls explain?" Watch "pls explain?" New topic

pls explain?

vikram choudhary .

Joined: Apr 25, 2007
Posts: 7
byte i= (byte)129;

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

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


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

So you have the byte


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


So the number is -127.
Chandra Bhatt
Ranch Hand

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


Consider Paul's rocket mass heater.
subject: pls explain?
It's not a secret anymore!