17

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;
}
einpoklum
  • 118,144
  • 57
  • 340
  • 684
001
  • 62,807
  • 94
  • 230
  • 350
  • 2
    this is cryptic code, not easy to read, with meaningless name for variables. `d = !( a > b && b <= c) || a > c && !b;` Please, program cleanly: Choose good names, and dont multiply numerous operation in one single line. – Stephane Rolland Nov 08 '10 at 12:00
  • 12
    @Stephane: "someone showed me this". If the questioner didn't write the code, how to carry out your instructions? As it happens, only the `!b` is relevant to the question, so the rest could be dropped, but since the questioner doesn't understand the `!` operator, this might not be obvious. – Steve Jessop Nov 08 '10 at 12:13
  • @Steve. Okay, but I have come across those variable names so often... – Stephane Rolland Nov 08 '10 at 12:18
  • 7
    @Stephane: in real code that might be a problem. In a question about syntax I certainly prefer `a`, `b`, `c`, `d` over names that relate to some specific programming task, and that might take effort to ignore. – Steve Jessop Nov 08 '10 at 12:28
  • @Steve, yep you are right... but there was also this !( a > b && b <= c) || a > c && !b; cryptic thing... It's not really about syntax, it's about uglyness ;-) But yes you are right for syntax only topic, abc do re mi maybe can do it – Stephane Rolland Nov 08 '10 at 12:52
  • @Stephane: I certainly agree that code is ugly, and I was quite glad the question wasn't, "please explain the whole of this horrible expression" :-) – Steve Jessop Nov 08 '10 at 12:57

5 Answers5

28

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.)

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
7

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.

Amnon
  • 7,652
  • 2
  • 26
  • 34
  • Sorry, I know I'm 8 years late, but do you have a quote from a C++ standard to back up your answer? –  Aug 03 '19 at 23:20
5

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

vitaut
  • 49,672
  • 25
  • 199
  • 336
3

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
3

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

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434