Hello,
For the snippet below
The output of the code was
tmp1=65535
tmp2=255
tmp3=65535
The binary value of 65535 is 1111 1111 1111 1111.
So when we do tmp2=(uint8)tmp 1, the upper 8 bits are not considered as it is typecasted to uint8.
So I understand tmp2 is assigned 255.
But when we do tmp3=(sint8)tmp 1 ,I dont know how the output of 65535 is produced?
(sint16)1111 1111 1111 1111===>0000 0001(2's complement of 1111 1111).
when this is assigned to tmp3 the output should have been 255.I dont understand how 65535 comes.
I guess the calculation goes as follows:
1111 1111 =============== =============== >0000 0001
2's complement of lower byte
0000 0001=========== ======>0000 0000 0000 0001
(32 bit notation)
0000 0000 0000 0001=========== >1111 1111 1111 1111
2's comp
[But I dont understand why the last 2's complement calculation is carried out.The above 3 calculations are just my ideas.If this idea is wrong please let me know where I am going wrong]
For the snippet below
Code:
unsigned int tmp1=65535; unsigned int tmp2=0,tmp3=0; tmp2=(char)tmp1; tmp3=(int)tmp1; printf("tmp1=%u\ntmp2=%u\ntmp3=%u\n",tmp1,tmp2,tmp3);
tmp1=65535
tmp2=255
tmp3=65535
The binary value of 65535 is 1111 1111 1111 1111.
So when we do tmp2=(uint8)tmp 1, the upper 8 bits are not considered as it is typecasted to uint8.
So I understand tmp2 is assigned 255.
But when we do tmp3=(sint8)tmp 1 ,I dont know how the output of 65535 is produced?
(sint16)1111 1111 1111 1111===>0000 0001(2's complement of 1111 1111).
when this is assigned to tmp3 the output should have been 255.I dont understand how 65535 comes.
I guess the calculation goes as follows:
1111 1111 =============== =============== >0000 0001
2's complement of lower byte
0000 0001=========== ======>0000 0000 0000 0001
(32 bit notation)
0000 0000 0000 0001=========== >1111 1111 1111 1111
2's comp
[But I dont understand why the last 2's complement calculation is carried out.The above 3 calculations are just my ideas.If this idea is wrong please let me know where I am going wrong]
Comment