In this C++ tutorial, you will learn about NOT Logical operator, its truth table, and how to use NOT logical operator with boolean values, with example programs.

C++ NOT Logical Operator

C++ NOT Logical Operator is used to inverse the result of a boolean condition. ! is the symbol used for NOT logical Operator.

NOT logical Operator takes only one boolean value as operand and returns a boolean value.

</>
Copy
!operand

If the operand evaluates to true, the NOT operator produces false. If the operand evaluates to false, it produces true. For this reason, ! is often described as negating or reversing a condition.

C++ NOT Operator Truth Table

Following is the truth table of NOT Logical Operator.

OperandReturns
true0
false1

Conceptually, the returned values are false and true. When a C++ bool is sent to cout without boolalpha, false is displayed as 0 and true is displayed as 1. This is why the table and the following program show numeric output.

Using C++ ! Operator with Boolean Values

Following example demonstrates the usage of NOT logical operator (!) with different boolean values.

main.cpp

</>
Copy
#include <iostream>
using namespace std;

int main() {
   cout << (!true) << endl;
   cout << (!false) << endl;
}

Output

0
1

In the first expression, true becomes false, which is printed as 0. In the second expression, false becomes true, which is printed as 1.

Using C++ NOT Operator in an if Condition

A common use of ! is to execute code when a boolean condition is false. For example, suppose a boolean variable indicates whether a user is logged in. The expression !isLoggedIn is true only when isLoggedIn is false.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    bool isLoggedIn = false;

    if (!isLoggedIn) {
        cout << "Please log in." << endl;
    }

    return 0;
}

Output

Please log in.

Here, isLoggedIn is false. Applying ! changes the result to true, so the body of the if statement runs.

C++ NOT Operator with Comparison Expressions

The operand of ! does not have to be a boolean variable. It can also be an expression that produces a boolean result. Parentheses can make the expression easier to read.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int age = 16;

    if (!(age >= 18)) {
        cout << "Age is below 18." << endl;
    }

    return 0;
}

Output

Age is below 18.

The comparison age >= 18 is false for the value 16. The NOT operator reverses that result to true.

C++ ! Operator with Non-Boolean Values

The logical NOT operator can also be applied to values that are contextually converted to bool. For integral values, zero is treated as false and a nonzero value is treated as true. The logical NOT operator then reverses that boolean result.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    cout << !0 << endl;
    cout << !5 << endl;
    cout << !-3 << endl;

    return 0;
}

Output

1
0
0

!0 produces true, while !5 and !-3 produce false. In most application code, using descriptive boolean expressions is clearer than relying on numeric truth values unless the numeric conversion is intentional.

C++ ! Operator and the not Alternative Token

C++ also provides not as an alternative token for the logical NOT operator. Therefore, not condition has the same logical meaning as !condition.

</>
Copy
!condition
not condition

The ! spelling is more commonly encountered in C++ source code, but both forms are part of the language.

Difference Between C++ ! and != Operators

The logical NOT operator ! and the not-equal operator != serve different purposes. ! is a unary operator and works on one operand. != is a comparison operator and compares two operands.

OperatorMeaningExample
!Reverses a boolean result!isReady
!=Tests whether two values are not equala != b

For example, if isReady is true, !isReady is false. By contrast, 10 != 20 is true because the two numbers are different.

Difference Between Logical NOT ! and Bitwise NOT ~ in C++

Do not confuse logical NOT ! with bitwise NOT ~. Logical NOT converts its operand to a boolean context and reverses that truth value. Bitwise NOT operates on the individual bits of an integral value.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int value = 5;

    cout << !value << endl;
    cout << ~value << endl;

    return 0;
}

Output

0
-6

Because 5 is nonzero, it is true in a boolean context and !5 becomes false, displayed as 0. The expression ~5, however, complements the bits of the integer and therefore performs a different operation.

C++ NOT Operator Precedence

Logical NOT is a unary operator and has higher precedence than comparison operators such as ==, !=, <, and >, and higher precedence than logical AND && and logical OR ||. When negating a complete comparison or compound condition, parentheses make the intended grouping explicit.

</>
Copy
!(x == 10)
!(x > 0 && x < 100)

In the second expression, the comparison results are combined with && first because of the parentheses, and ! then reverses the result of the entire compound condition.

Double Logical NOT !! in C++

Applying logical NOT twice converts a value to its boolean equivalent. The first ! reverses the truth value, and the second reverses it again.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    cout << !!0 << endl;
    cout << !!25 << endl;

    return 0;
}

Output

0
1

This technique can appear in existing code, but an explicit boolean expression is often easier to understand when readability is the main concern.

Common Mistakes When Using C++ Logical NOT

  • Confusing ! with !=: ! negates one condition, while != compares two values for inequality.
  • Confusing ! with ~: ! performs logical negation; ~ performs bitwise complement on an integral value.
  • Negating only part of a condition: use parentheses when you intend to negate a complete comparison or compound expression, such as !(a > 0 && b > 0).
  • Misreading 0 and 1 in output: a boolean printed with ordinary cout appears as 0 or 1 unless boolean text formatting is enabled.

C++ NOT Operator Editorial QA Checklist

  • Confirm that ! is described as unary logical negation and not as the not-equal operator.
  • Confirm that the truth-table explanation distinguishes the boolean values true/false from their default cout display as 1/0.
  • Check that examples using compound conditions clearly show which expression is being negated.
  • Keep logical NOT ! separate from bitwise NOT ~.
  • Verify that any discussion of non-boolean operands explains their conversion to a boolean context before logical negation.
  • Use != only when comparing two operands for inequality.

Conclusion

In this C++ Tutorial, we learned what C++ NOT Logical Operator is, and how to use it with conditional expressions.

The key rule is straightforward: ! reverses the boolean result of its operand. It can be used with boolean variables, comparisons, and compound conditions, while != and ~ are separate operators with different purposes.