C++ NOT(~) vs NEGATION(!)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11572181/
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
NOT(~) vs NEGATION(!)
提问by tez
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int i=-5;
while(~(i))
{
cout<<i;
++i;
}
}
The output is -5,-4,-3,-2. Shouldn't it print values till -1?Why is it only till -2. And please explain me the difference between 'not' and 'negation' operators.When ever I write a program they were the source for bugs.
输出为 -5,-4,-3,-2。它不应该打印值到 -1 吗?为什么只打印到 -2。请向我解释“非”和“否定”运算符之间的区别。当我编写程序时,它们都是错误的来源。
while(i)
I know that the loop condition will be true for positive and negative i's except 0.
我知道循环条件对于正负 i 都是真的,除了 0。
while(!i) vs while(~i)
For what values of 'i' the above two loops get executed?
对于 'i' 的什么值,上述两个循环被执行?
回答by Ry-
When i
gets to -1
, the value of ~i
is ~-1
, or 0
, so the while
loop stops executing. The !
operator works because it does something completely different; it results in 1
for 0
values and 0
for all other values. ~
is a bitwise negation.
当i
到达-1
,的值~i
是~-1
,或者0
,所以while
循环停止执行。该!
运营商工作,因为它会产生完全不同的; 它导致1
了0
价值观和0
所有其他值。~
是按位否定。
A little more in detail:
再详细一点:
~
takes each bit in a number and toggles it. So, for example, 100102would become 011012-1
is all ones in binary when a two's complement signed integer.~0b…11111111
is0
.
~
取数字中的每一位并切换它。因此,例如,10010 2将变为 01101 2-1
当二进制补码有符号整数时,所有二进制都是 1。~0b…11111111
是0
。
However:
然而:
!0
is1
,!anythingElse
is0
-1
is not0
!-1
is still0
!0
是1
,!anythingElse
是0
-1
不是0
!-1
还是0
And if you actually want to loop including i == -1
, just use while (i)
instead of while (~i)
.
如果你真的想循环包括i == -1
,只需使用while (i)
代替while (~i)
。
回答by dasblinkenlight
You are correct about i == -1
being the exit condition: your loop is equivalent to
你是正确i == -1
的退出条件:你的循环相当于
int i=-5;
while(i != -1)
{
cout<<i;
++i;
}
// i == -1 immediately after the loop
When written this way, it should be clear why -1
is not printed the value is first printed, and only then incremented, that's why -2
is the last value that you print.
以这种方式编写时,应该清楚为什么-1
不打印该值首先打印,然后才递增,这就是-2
您打印的最后一个值的原因。
The !
operator, on the other hand, will produce 1
only when it is given a zero. That's why the loop would print -1
when the !
operator is used in the loop condition.
该!
运营商,而另一方面,会产生1
只有当它被赋予一个零。这就是为什么在循环条件中使用运算符-1
时循环会打印的原因!
。
回答by aasa
'~' is the operator that : ~x = -x-1 and when i = -1, then ~i = 0. if you wonder the value of ~i, you can just print them out:
'~' 是操作符:~x = -x-1,当 i = -1 时,~i = 0。如果你想知道 ~i 的值,你可以打印出来:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int i=-5;
for (int i = -5; i <= 3; i++)
{
cout<<i<<" "<<(~i)<<endl;
}
}
and then you will find: -5 4 -4 3 -3 2 -2 1 -1 0 0 -1 1 -2 2 -3 3 -4
然后你会发现: -5 4 -4 3 -3 2 -2 1 -1 0 0 -1 1 -2 2 -3 3 -4
回答by Steve Taylor
!
is true/false logic flipping
!
是真/假逻辑翻转
!
means any nonzero becomes 0
, and 0 becomes 1
!
意味着any nonzero becomes 0
,和0 becomes 1
eg1. !0b1010 -> 0b0000
例如1。 !0b1010 -> 0b0000
eg2. !0b0000 -> 0b0001
例如2。 !0b0000 -> 0b0001
eg3. !0b1111 -> 0b0000
例如3。 !0b1111 -> 0b0000
generalised, out = in?0:1
概括, out = in?0:1
while...
尽管...
~
is bit flipping
~
有点翻转
~
means flip each and every bit
~
方法 flip each and every bit
eg1. ~0b1010 -> 0b0101
例如1。 ~0b1010 -> 0b0101
eg2. ~0b0000 -> 0b1111
例如2。 ~0b0000 -> 0b1111
eg3. ~0b1111 -> 0b0000
例如3。 ~0b1111 -> 0b0000
generalised, out = in^0b1111
概括, out = in^0b1111