# casting problem...from long to byte..

amit taneja

Ranch Hand

Posts: 813

posted 11 years ago

- 0

Hi,

I m preparing for scjp and refering book by bertbates/kathy seera...

I m not able to clear one funda that while explicit casting from long to byte... when the value is greater then +127.

in that book ... in one page there is written about removing of higher order bits left to 8 lower order bits...and if last bit left in that 8 bits is 1 then the no. will be negative of remaining 7 bits

but in other page its witten about complement notation... i.e to find out the complement of the no. and add 1 to it..and then find out the final no. stored in byte...?

can any body pls tell me whats is all about and what is truth ..and even when we follow complement startergy but why so ???

answer in details ..

thanx and regards,

amit

I m preparing for scjp and refering book by bertbates/kathy seera...

I m not able to clear one funda that while explicit casting from long to byte... when the value is greater then +127.

in that book ... in one page there is written about removing of higher order bits left to 8 lower order bits...and if last bit left in that 8 bits is 1 then the no. will be negative of remaining 7 bits

but in other page its witten about complement notation... i.e to find out the complement of the no. and add 1 to it..and then find out the final no. stored in byte...?

can any body pls tell me whats is all about and what is truth ..and even when we follow complement startergy but why so ???

answer in details ..

thanx and regards,

amit

Thanks and Regards, Amit Taneja

Steven Bell

Ranch Hand

Posts: 1071

posted 11 years ago

- 0

My guess is that the book is saying the following.

If you cast a long to a byte and the 8th bit is a 1 that number will be negative.

In order to find out the value of that negative number you must take the two's complement of the number.

It's been a couple years since I've done two's complement, but I think it goes something like flip the bits and add 1, or add 1 and flip the bits. I'm sure somebody here remembers how to do it.

If you cast a long to a byte and the 8th bit is a 1 that number will be negative.

In order to find out the value of that negative number you must take the two's complement of the number.

It's been a couple years since I've done two's complement, but I think it goes something like flip the bits and add 1, or add 1 and flip the bits. I'm sure somebody here remembers how to do it.

posted 11 years ago

- 0

See this page on two's complement...

http://mathforum.org/library/drmath/view/54344.html

http://mathforum.org/library/drmath/view/54344.html

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

I agree. Here's the link: http://aspose.com/file-tools |