简体   繁体   English

否定整数会产生意外结果

[英]Negation of an integer is giving unexpected result

I am unable to understand how the following code gives -12. 我无法理解以下代码如何给出-12。 Please help me. 请帮我。

int a=11;    
int result=~a;    
System.out.println(result);    

I thought it will give 4. but yes I saw a preceding zero. 我以为会得到4。但是是的,我看到了前面的零。
I guess it has got something to do with 2's complement but cant makes out how. 我想这与2的补码有关,但无法说明。

11 's binary representation is 00000000000000000000000000001011 . 11的二进制表示形式是00000000000000000000000000001011

The negation of that number is 11111111111111111111111111110100 . 该数字的取反是11111111111111111111111111110100

That's the 2's complement representation of -12 . 那是-12的2的补码表示形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM