somehow, Im missing something about this situation:
1) I know that the maximum value of a byte is 127 because a byte is 8 bits and max value is (2^7)-1 and min value is -(2^7)
2) I know that
Java allows you to convert the long to a byte but that you can get an "unexpected" result because of considerations for two's compliment implementation and oveflows and other stuff that Michael Ernest talked about in the topic
values cast to a byte 3) I know that I have to remember that when the long value of 130 casts to a byte, we hit 127 then have to start counting up from -128,-127, -126 (stop) to arrive at correct answer.
4)
here's where I don't follow: 130 L represented as binary is 64 bits:
leading zeroes ... 1000 0010 <- last 8 bits
to get the byte, we retain only last 8 bits
1000 0010
as I look at this, the leftmost bit tells me it's
a negative number;
so to store this negative number:
a) we first take one's compliment:
0111 1101
b) add 1 to this
0111 1101
+0000 0001
_________
=0111 1110
this looks like positive 126 to me.
where I am goofing up? Your help is appreciated.
[ November 21, 2002: Message edited by: david eberhardt ]