Java 按位非运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2513525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:41:21  来源:igfitidea点击:

bitwise not operator

javajavascriptc++ctwos-complement

提问by Sawyer

Why bitwise operation (~0);prints -1 ? In binary , not 0 should be 1 . why ?

为什么按位运算(~0);打印 -1 ?在二进制中,不是 0 应该是 1。为什么 ?

采纳答案by polygenelubricants

You are actually quite close.

你实际上很接近。

In binary , not 0 should be 1

在二进制中,不是 0 应该是 1

Yes, this is absolutely correct when we're talking about one bit.

是的,当我们谈论一点时,这是绝对正确的。

HOWEVER, an intwhose value is 0 is actually 32 bits of all zeroes! ~inverts all 32 zeroes to 32 ones.

然而,一个int值为 0 的实际上是 32 位全零!~将所有 32 个零反转为 32 个 1。

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 intin two's complement representation, ~1 == -2.

也就是说,对于以int二进制补码表示的 32 位无符号数,~1 == -2.



Further reading:

进一步阅读:

回答by Bombe

Because ~is not binary inversion, it's bitwise inversion. Binary inversion would be !and can (in Java) only be applied to boolean values.

因为~不是二进制反转,而是按位反转。二进制反转将!并且可以(在 Java 中)仅应用于布尔值。

回答by cletus

In standard binary encoding, 0 is all 0s, ~is bitwise NOT. All 1s is (most often) -1 for signed integer types. So for a signed byte type:

在标准二进制编码中,0 全为 0,~按位非。对于有符号整数类型,所有 1 都是(最常见的)-1。所以对于有符号字节类型:

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

回答by Daniel Fath

It's binary inversion, and in second complement -1 is binary inversion of 0.

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

回答by LaZe

What you are actually saying is ~0x00000000 and that results in 0xFFFFFFFF. For a (signed) int in java, that means -1.

你实际上是在说 ~0x00000000,结果是 0xFFFFFFFF。对于 java 中的(有符号的)int,这意味着 -1。

回答by N 1.1

~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
1 0 0 0 0 0 0 0 = ?128

0 1 1 1 1 1 1 0 = 126
1 0 0 0 0 0 0 1 = ?127

1 1 1 1 1 1 1 1 = ?1
0 0 0 0 0 0 0 0 = 0

1 1 1 1 1 1 1 0 = ?2
0 0 0 0 0 0 0 1 = 1

1 0 0 0 0 0 0 1 = ?127
0 1 1 1 1 1 1 0 = 126

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
1 0 0 0 0 0 0 0 = ?128

0 1 1 1 1 1 1 0 = 126
1 0 0 0 0 0 0 1 = ?127

1 1 1 1 1 1 1 1 = ?1
0 0 0 0 0 0 0 0 = 0

1 1 1 1 1 1 1 0 = ?2
0 0 0 0 0 0 0 1 = 1

1 0 0 0 0 0 0 1 = ?127
0 1 1 1 1 1 1 0 = 126

1 0 0 0 0 0 0 0 = ?128
0 1 1 1 1 1 1 1 = 127

回答by kpower

0 here is not a bit. It is a byte (at least; or more) - 00000000. Using bitwise or we will have 11111111. It is -1 as signed integer...

0这里不是一点。它是一个字节(至少;或更多) - 00000000。使用按位或我们将有 11111111。它是 -1 作为有符号整数......

回答by YOU

For 32 bit signed integer

对于 32 位有符号整数

~00000000000000000000000000000000=11111111111111111111111111111111(which is -1)

~00000000000000000000000000000000=11111111111111111111111111111111(这是-1)

回答by nickf

You could imagine the first bit in a signed number to be -(2x -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!

回答by Nacho

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 指定字符代字号 ~ 表示二进制补码,尽管在大多数编程语言中,代字号代表一个二进制补码的切换。