A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Programmer Certification (SCJP/OCPJP)
vikram choudhary .
Joined: Apr 25, 2007
Apr 25, 2007 22:13:00
byte i= (byte)129;
output: -127 why?
can anyone explain?
Joined: Feb 07, 2005
Apr 25, 2007 22:33:00
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
So the number is -127.
Joined: Feb 28, 2007
Apr 25, 2007 22:38:00
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: pls explain?
isn't this strange?
Casting int to byte (was: pls explain)
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015