简体   繁体   English

按位不是运算符

[英]bitwise not operator

Why bitwise operation (~0); 为什么按位运算(~0); prints -1 ? 打印-1? In binary , not 0 should be 1 . 在二进制中,不应该是1。 why ? 为什么?

You are actually quite close. 你其实非常接近。

In binary , not 0 should be 1 在二进制中,不应该是1

Yes, this is absolutely correct when we're talking about one bit. 是的,当我们谈论一点时,这是完全正确的。

HOWEVER, an int whose value is 0 is actually 32 bits of all zeroes! 但是,一个值为0的int实际上是所有零的32位! ~ inverts all 32 zeroes to 32 ones. ~所有32个零反转为32个。

System.out.println(Integer.toBinaryString(~0));
// prints "11111111111111111111111111111111"

This is the two's complement representation of -1 . 这是-1的二进制补码表示。

Similarly: 同理:

System.out.println(Integer.toBinaryString(~1));
// prints "11111111111111111111111111111110"

That is, for a 32-bit unsigned int in two's complement representation, ~1 == -2 . 也就是说,对于二进制补码表示中的32位无符号int~1 == -2


Further reading: 进一步阅读:

What you are actually saying is ~0x00000000 and that results in 0xFFFFFFFF. 你实际上说的是~0x00000000并导致0xFFFFFFFF。 For a (signed) int in java, that means -1. 对于java中的(带符号)int,这意味着-1。

You could imagine the first bit in a signed number to be -(2 x -1 ) where x is the number of bits. 你可以想象有符号数中的第一位是 - (2 x -1 ),其中x是位数。

So, given an 8-bit number, the value of each bit (in left to right order) is: 因此,给定一个8位数字,每个位的值(按从左到右的顺序)是:

-128 64 32 16 8 4 2 1

Now, in binary, 0 is obviously all 0s: 现在,在二进制中,0显然都是0:

    -128 64 32 16 8 4 2 1
0      0  0  0  0 0 0 0 0 = 0

And when you do the bitwise not ~ each of these 0s becomes a 1: 而当你做的按位取反~每一个0变成1:

     -128 64 32 16 8 4 2 1
~0      1  1  1  1 1 1 1 1
 =   -128+64+32+16+8+4+2+1 == -1

This is also helpful in understanding overflow: 这也有助于理解溢出:

     -128 64 32 16 8 4 2 1
126     0  1  1  1 1 1 1 0  =  126
 +1     0  1  1  1 1 1 1 1  =  127
 +1     1  0  0  0 0 0 0 0  = -128  overflow!

~ is a bitwise operator. ~是一个按位运算符。

~0 = 1 which is -1 in 2's complement form  

http://en.wikipedia.org/wiki/Two's_complement http://en.wikipedia.org/wiki/Two's_complement

Some numbers in two's complement form and their bit-wise not ~ (just below them): 有些数字是二进制补码形式,而有些数字不是~ (就在它们之下):

0 1 1 1 1 1 1 1 = 127 0 1 1 1 1 1 1 1 = 127
1 0 0 0 0 0 0 0 = −128 1 0 0 0 0 0 0 0 = -128

0 1 1 1 1 1 1 0 = 126 0 1 1 1 1 1 1 0 = 126
1 0 0 0 0 0 0 1 = −127 1 0 0 0 0 0 0 1 = -127

1 1 1 1 1 1 1 1 = −1 1 1 1 1 1 1 1 1 = -1
0 0 0 0 0 0 0 0 = 0 0 0 0 0 0 0 0 0 = 0

1 1 1 1 1 1 1 0 = −2 1 1 1 1 1 1 1 0 = -2
0 0 0 0 0 0 0 1 = 1 0 0 0 0 0 0 0 1 = 1

1 0 0 0 0 0 0 1 = −127 1 0 0 0 0 0 0 1 = -127
0 1 1 1 1 1 1 0 = 126 0 1 1 1 1 1 1 0 = 126

1 0 0 0 0 0 0 0 = −128 1 0 0 0 0 0 0 0 = -128
0 1 1 1 1 1 1 1 = 127 0 1 1 1 1 1 1 1 = 127

Because ~ is not binary inversion, it's bitwise inversion. 因为~不是二进制反转,所以它是按位反转。 Binary inversion would be ! 二进制反转将是! and can (in Java) only be applied to boolean values. 并且可以(在Java中)仅应用于布尔值。

In standard binary encoding, 0 is all 0s, ~ is bitwise NOT. 在标准二进制编码中,0全为0, ~为按位NOT。 All 1s is (most often) -1 for signed integer types. 对于有符号整数类型,所有1都是(最常见的)-1。 So for a signed byte type: 所以对于有符号的字节类型:

0xFF = -1    // 1111 1111
0xFE = -2    // 1111 1110
...
0xF0 = -128  // 1000 0000
0x7F = 127   // 0111 1111
0x7E = 126   // 0111 1110
...
0x01 = 1     // 0000 0001
0x00 = 0     // 0000 0000

I think the real reason is that ~ is Two's Complement. 我认为真正的原因是〜是Two's Complement。

Javascript designates the character tilde, ~, for the two's complement, even though in most programming languages tilde represents a bit toggle for the one's complement. Javascript指定字符代字号〜,对于二进制补码,即使在大多数编程语言中,代字号表示一个补码的位切换。

它是二进制反转,在第二个补码-1中是二进制反转0。

0 here is not a bit. 0这里没有一点。 It is a byte (at least; or more) - 00000000. Using bitwise or we will have 11111111. It is -1 as signed integer... 它是一个字节(至少;或更多) - 00000000。使用按位或我们将有11111111.它是-1作为有符号整数...

For 32 bit signed integer 对于32位有符号整数

~00000000000000000000000000000000=11111111111111111111111111111111 (which is -1) ~00000000000000000000000000000000=11111111111111111111111111111111 (-1)

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

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