开发者

One' s Complement of a number

suppose i have a variable

int a=512

Now when i apply ~a which means one's complement in c.How i am getting -5开发者_运维百科13 as the output.

int is minimum 16 bits in size and binary equivalent of 512 is

0000 0010 0000 0000 And now when you do its one's complement it becomes

1111 1101 1111 1111. Now could someone tell me why the output is -513.

Can I now Hope that guys in this forum understood my question.


Negative numbers are often represented as two's complement, which is why it is interpreted the way it is.

Assuming int is 16-bit, you will have the bitpattern 1111 1101 1111 1111. Since the top-most bit is set, the number is negative. To convert to decimal, according to the rules for two's complement, the number is first bitwise-inverted. This brings back your 512. Then one is added, which gives 513. And since the number was negative, we get -513.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜