Originally posted by kantu Deshpande:
Hi!!,
byte b;
int i=257;
[b]b= (byte)i;
If I type cast as shown above, I'm getting answer b=1. But, if I intialize int i=144 I'm getting the answer b=-112. Can any body explain how this type casting been done. Please if you give me one example, I would really appreciate.
Thanks,
Kantu
[/B]
int i=257 is represented as 0000 0001 0000 0001
when you cast an int into byte only the lower order bits are copied
so byte b= 0000 00001 which is 1.
int i=144 is represented as 0000 0000 1001 0000
1001 0000 (in 2's complement form) represents -112.
-------------------
(to get the 2's complement of 112)
112 = 0111 0000
1' complement = 1000 1111
add 1 = 1001 0000
[ for more info search this forum for the words - 2's complement ]
[This message has been edited by Alagu Seenivasan (edited October 05, 2000).]