I understand that the output 655535 is caused due to sign extension in statement (1), but what is happening in statement (2) that prevents this. Width of byte is 8 bits, so 'anding' it with 0xff should just result in byte value unchanged, shouldn't it?
BTW how can I print 'byte' as binary or hex strings?
If you need more explanation, please ask again.
char . . . values are 16-bit unsigned integers
santhosh.R gowda wrote:
char c= (char)(b); //(1)
System.out.println((int)c); //output: 65535
Can you please tell me how we get the out put 65535 i'm confused
Easy part, a byte is signed 8 bit value, and a char is a unsigned 16 bit value. Hard part, you need to understand how Java stores negative numbers... see twos complement...
Once you understand twos complement, and how a byte is sign extended when casting to a char, it should be straightforward. However, if you are still confused (after you get up to speed on twos complement), come back here, and elaborate what you don't understand.
santhosh.R gowda wrote:Dear all
how come this even though the byte has to sign extension like char in above
b = 1111 1111
i (sign extend b to length of int) = 1111 1111 1111 1111 1111 1111 1111 1111
Remember an int is signed, and a char isn't (when applying twos complement)....