Java: ~ 是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1483504/
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
Java: What does ~ mean
提问by Martijn Courteaux
In this Java source code I have this line:
在这个 Java 源代码中,我有这一行:
if ((modifiers & ~KeyEvent.SHIFT_MASK) != 0) ....
What does the tilde ~
mean?
波浪号~
是什么意思?
采纳答案by Richard
The Tilde (~
) performs a bitwise complement of a numerical value in Java.
波浪号 ( ~
) 在 Java 中执行数值的按位补码。
See: Bitwise complement (~
): inverts ones and zeroes in a number
回答by Pascal MARTIN
It is the Unary ~ Bitwise complementoperator (quoting):
它是一元 ~ 按位补码运算符(引用):
- only used with integer values
- inverts the bits ie a 0-bit becomes 1-bit and vice versa
- in all cases ~x equals (-x)-1
- 仅用于整数值
- 反转位,即 0 位变为 1 位,反之亦然
- 在所有情况下 ~x 等于 (-x)-1
See also this page on Bitwise operators on wikipedia, which states :
另请参阅维基百科上有关按位运算符的此页面,其中指出:
The bitwise NOT, or complement, is a unary operation that performs logical negation on each bit, forming the ones' complement of the given binary value. Digits which were 0 become 1, and vice versa.
For example:
按位非或补码是一种一元运算,它对每一位执行逻辑否定,形成给定二进制值的补码。0 的数字变为 1,反之亦然。
例如:
NOT 0111 (decimal 7)
= 1000 (decimal 8)
In many programming languages (including those in the C family), the bitwise NOT operator is "
~
" (tilde).
在许多编程语言 (包括 C 系列中的那些)中,按位非运算符是“
~
”(波浪号)。
回答by Alberto Zaccagni
From the official docs http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html:
从官方文档http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html:
The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".
一元按位补码运算符“~”反转位模式;它可以应用于任何整数类型,使每个“0”成为“1”,每个“1”成为“0”。例如,一个字节包含 8 位;将此运算符应用于位模式为“00000000”的值会将其模式更改为“11111111”。
回答by VolkerK
As said before ~
is the unary bitwise NOT operator.
Your example tests whether modifiers
contains bits other than those defined in KeyEvent.SHIFT_MASK
.
如前所述~
是一元按位非运算符。
您的示例测试是否modifiers
包含中定义的位以外的位KeyEvent.SHIFT_MASK
。
~KeyEvent.SHIFT_MASK
-> all bits except those in KeyEvent.SHIFT_MASK are set to 1.(modifiers & ~KeyEvent.SHIFT_MASK)
-> every 1-bit inmodifiers
that "does not belong" toKeyEvent.SHIFT_MASK
if ((modifiers & ~KeyEvent.SHIFT_MASK) != 0)
-> if there was at least one other bit set to 1 besidesKeyEvent.SHIFT_MASK
do something...
~KeyEvent.SHIFT_MASK
-> 除 KeyEvent.SHIFT_MASK 中的位之外的所有位都设置为 1。(modifiers & ~KeyEvent.SHIFT_MASK)
->modifiers
“不属于”的每一位KeyEvent.SHIFT_MASK
if ((modifiers & ~KeyEvent.SHIFT_MASK) != 0)
-> 如果除了KeyEvent.SHIFT_MASK
做某事之外,至少还有一个其他位设置为 1 ...
回答by Dhwaneel
From Java's website http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
从 Java 的网站http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".
一元按位补码运算符“~”反转位模式;它可以应用于任何整数类型,使每个“0”成为“1”,每个“1”成为“0”。例如,一个字节包含 8 位;将此运算符应用于位模式为“00000000”的值会将其模式更改为“11111111”。
Now, as previously answered by Pascal MARTIN, at any given case the value equals to -(x)-1. E.g. ~2=-3, ~-6=5, etc.
现在,正如 Pascal MARTIN 先前回答的那样,在任何给定情况下,该值都等于 -(x)-1。例如~2=-3,~-6=5,等等。
Also, in java all positive integersare stored as their binary representations and negative integersare stored in 2's compliment value of a positive integer.
此外,在 java 中,所有正整数都存储为它们的二进制表示,负整数存储在正整数的 2 的补码值中。
Now, let's see how it works in bit level in case of ~2=-3:
现在,让我们看看在 ~2=-3 的情况下它是如何在位级工作的:
Initially, 2 is stored in its binary representation:
最初,2 以其二进制表示形式存储:
0000 0000 0000 0010
Now ~2 will result in the value (inverse the bits):
现在 ~2 将导致值(反转位):
1111 1111 1111 1101
How in the world I know it is -3? Well, it is -3 because it is derived from 2's compliment representation of 3.
我怎么知道它是-3?嗯,它是 -3,因为它是从 3 的 2 的恭维表示派生出来的。
As we know 2's(x)= 1's(x) + 1 (https://delightlylinux.wordpress.com/2014/10/13/binary-lesson-12-ones-complement-and-twos-complement/)
Our aim is it to find x:
1's(x)= 2's(x) - 1 (based on previous expression)
我们知道 2's(x)= 1's(x) + 1 ( https://delightlylinux.wordpress.com/2014/10/13/binary-lesson-12-ones-complement-and-twos-complement/)
我们的目标是否要找到 x:
1's(x)= 2's(x) - 1(基于前面的表达式)
As our answer is in is in 2's compliment,
1's(x)= 1111 1111 1111 1101 - 0000 0000 0000 0001
1's (x)= 1111 1111 1111 1100
(How to subtract -http://sandbox.mc.edu/~bennet/cs110/pm/sub.html)
Therefore x= 1's compliment of value (as the answer we got represents 1's compliment of x).
x = 0000 0000 0000 0011
So, we have found that x is 3 and hence our previous result of ~ operator 1111 1111 1111 1101
is -3 written as 2's compliment of 3.
由于我们的答案是 2 的恭维,
1's(x)= 1111 1111 1111 1101 - 0000 0000 0000 0001
1's (x)= 1111 1111 1111 1100
(如何减去 - http://sandbox.mc.edu/~bennet/cs110/pm/sub.html)
因此 x= 1价值的赞美(因为我们得到的答案代表 1 对 x 的赞美)。
x =0000 0000 0000 0011
所以,我们发现 x 是 3,因此我们之前的 ~ 运算符的结果1111 1111 1111 1101
是 -3 写成 2 的 3 的补码。