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

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

NOT(~) vs NEGATION(!)

c++cbitwise-operatorslogical-operators

提问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 igets to -1, the value of ~iis ~-1, or 0, so the whileloop stops executing. The !operator works because it does something completely different; it results in 1for 0values and 0for all other values. ~is a bitwise negation.

i到达-1,的值~i~-1,或者0,所以while循环停止执行。该!运营商工作,因为它会产生完全不同的; 它导致10价值观和0所有其他值。~是按位否定。

A little more in detail:

再详细一点:

  • ~takes each bit in a number and toggles it. So, for example, 100102would become 011012
  • -1is all ones in binary when a two's complement signed integer.
  • ~0b…11111111is 0.
  • ~取数字中的每一位并切换它。因此,例如,10010 2将变为 01101 2
  • -1当二进制补码有符号整数时,所有二进制都是 1。
  • ~0b…111111110

However:

然而:

  • !0is 1, !anythingElseis 0
  • -1is not 0
  • !-1is still 0
  • !01!anythingElse0
  • -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 == -1being 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 -1is not printed the value is first printed, and only then incremented, that's why -2is the last value that you print.

以这种方式编写时,应该清楚为什么-1不打印该值首先打印,然后才递增,这就是-2您打印的最后一个值的原因。

The !operator, on the other hand, will produce 1only when it is given a zero. That's why the loop would print -1when 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