2's compliment to decimal value..?

Hi All, byte b=127; b<<=1; Here b is promoted to int and multiplied by 2 and result is 254.Then it is implicitely type casted to byte and it contains 11111110bits,so in byte it represents -ve value and it is in 2's compliment form.Then, what is simplest way to find its decimal value.?

Originally posted by Prashant Neginahal: Hi All, byte b=127; b<<=1; Here b is promoted to int and multiplied by 2 and result is 254.Then it is implicitely type casted to byte and it contains 11111110bits,so in byte it represents -ve value and it is in 2's compliment form.Then, what is simplest way to find its decimal value.?

cast to byte: 1111 1110 = 254 is it not the answer? why do we have to do those calculation as u suggest? can u please explain? Thanks Karthik

For byte, the range is from -2^7 to 2^7 - 1. So, -128 <= byte <= 127. So 254 is out of raange for byte in the first place. 127 = 0111 1111 So, for positive, the highest is always 0. And negative is always 1 in highest bite. The rest is according to definition. Just follow my procedure in previous post.