我可以在 C++ 中对 int 值使用 not 运算符吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4123550/
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-28 14:34:50  来源:igfitidea点击:

Can I use the not operator in C++ on int values?

c++intlogical-operatorsnegation

提问by 001

Strange question, but someone showed me this, I was wondering can you use the not ! operator for int in C++? (its strange to me).

奇怪的问题,但有人给我看了这个,我想知道你能不能使用 not !C++中int的运算符?(这对我来说很奇怪)。

#include <iostream>
using namespace std;

int main()
{
   int a=5, b=4, c=4, d;
   d = !( a > b && b <= c) || a > c && !b;
   cout << d;
   system ("pause");
   return 0;
}

回答by kennytm

Yes. For integral types, !returns trueif the operand is zero, and falseotherwise.

是的。对于整数类型,如果操作数为零则!返回truefalse否则返回。

So !bhere just means b == 0.

所以!b这里只是意味着b == 0.



This is a particular case where a value is converted to a bool. The !bcan be viewed as !((bool)b)so the question is what is the "truthness" of b. In C++, arithmetic types, pointer types and enum can be converted to bool. When the value is 0 or null, the result is false, otherwise it is true(C++ §4.1.2).

这是将值转换为 的特殊情况bool。该!b可被视为!((bool)b)这样的问题是什么的“truthness” b。在 C++ 中,算术类型、指针类型和枚举可以转换为bool. 当值为 0 或 null 时,结果为false,否则为true(C++ §4.1.2)。

Of course custom classes can even overload the operator!or operator<types can be convert to bool> to allow the !bfor their classes. For instance, std::streamhas overloaded the operator!and operator void*for checking the failbit, so that idioms like

当然,自定义类甚至可以重载operator!operator<类型可以转换为 bool> 以允许!b它们的类。例如,std::stream重载了operator!andoperator void*来检查故障位,所以像这样的习语

while (std::cin >> x) {   // <-- conversion to bool needed here
  ...

can be used.

可以使用。

(But your code !( a > b && b <= c) || a > c && !bis just cryptic.)

(但你的代码!( a > b && b <= c) || a > c && !b很神秘。)

回答by Amnon

Originally, in C (on which C++ is based) there was no Boolean type. Instead, the value "true" was assigned to any non-zero value and the value "false" was assigned to anything which evaluates to zero. This behavior still exists in C++. So for an int x, the expressions !xmeans "xnot true", which is "xnot non-zero", i.e. it's true if xis zero.

最初,在 C(C++ 所基于的)中没有布尔类型。相反,值“真”被分配给任何非零值,值“假”被分配给任何评估为零的值。这种行为在 C++ 中仍然存在。因此,对于 an int x,表达式的!x意思是“x不为真”,即“x非非零”,即如果x为零则为真。

回答by vitaut

You can, !bis equivalent to (b == 0).

可以,!b相当于(b == 0)

回答by Armen Tsirunyan

The build-in !operator converts its argument to bool. The standard specifies that there exists a conversion from any arithmetic type(int, char,.... float, double...) to bool. If the source value is 0 the result is true, otherwise it is false

内置!运算符将其参数转换为bool. 该标准指定存在从任何算术类型( int, char,.... float, double...) 到 bool 的转换。如果源值为 0,则结​​果为true,否则为false

回答by Puppy

The test for int is true for non-zero values and false for zero values, so not is just true for zero values and false for non-zero values.

int 的测试对于非零值是真的,对于零值是假的,所以不仅零值是真的,非零值是假的。