Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

#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;
}
like image 682
001 Avatar asked Nov 08 '10 11:11

001


People also ask

What is logical NOT operator in C?

Logical NOT (!) operator in C Logical NOT is denoted by exclamatory characters (!), it is used to check the opposite result of any given test condition. If any condition's result is non-zero (true), it returns 0 (false) and if any condition's result is 0 (false) it returns 1 (true). Syntax of Logical NOT operator:

What is the use of the “not” operator in a circuit?

! is a type of Logical Operator and is read as “ NOT ” or “ Logical NOT “. This operator is used to perform “logical NOT” operation, i.e. the function similar to Inverter gate in digital electronics.

What is the Boolean type of INT in C++?

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 !x means "x not true", which is "x not non-zero", i.e. it's true if x is zero.

What is the not in operator in SQL Server?

The SQL Server NOT IN operator is used to replace a group of arguments using the <> (or !=) operator that are combined with an AND. It can make code easier to read and understand for SELECT, UPDATE or DELETE SQL commands. Generally, it will not change performance characteristics. Consider this SQL query:


Video Answer


2 Answers

Yes. For integral types, ! returns true if the operand is zero, and false otherwise.

So !b here just means b == 0.


This is a particular case where a value is converted to a bool. The !b can 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).

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

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

can be used.

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

like image 194
kennytm Avatar answered Sep 30 '22 02:09

kennytm


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 !x means "x not true", which is "x not non-zero", i.e. it's true if x is zero.

like image 35
Amnon Avatar answered Sep 30 '22 02:09

Amnon