Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of the NOT operator? [closed]

Tags:

c++

Currently checking out C++ using this tutorial. It explains what the ! NOT operator is, kind of, but I don't think I fully understand why I'd ever want to use it. Can someone please explain?

like image 703
Danny Avatar asked Mar 25 '13 16:03

Danny


People also ask

What is the point of the NOT operator?

What Does NOT Operator Mean? In Boolean algebra, the NOT operator is a Boolean operator that returns TRUE or 1 when the operand is FALSE or 0, and returns FALSE or 0 when the operand is TRUE or 1. Essentially, the operator reverses the logical value associated with the expression on which it operates.

What does the NOT operator do in Java?

The not operator is a logical operator, represented in Java by the ! symbol. It's a unary operator that takes a boolean value as its operand. The not operator works by inverting (or negating) the value of its operand.

What does the logical operator NOT MEAN?

The logical NOT ( ! ) operator (logical complement, negation) takes truth to falsity and vice versa. It is typically used with boolean (logical) values. When used with non-Boolean values, it returns false if its single operand can be converted to true ; otherwise, returns true .

What is not operator with example?

Similarly, if the condition's result is false or 0, the NOT operator reverses the result and returns 1 or true. For example, suppose the user enters a non-zero value is 5, the logical NOT (!) operator returns the 0 or false Boolean value.


2 Answers

The ! operator is useful if you want to check that a condition is not currently satisfied.

Imagine a function that tells you if a particular subsystem of your application was initialized and another function to initialize it:

bool subsystem_is_initialized();
void subsystem_initialize();

You could check that it was initialized and initialize it if it wasn't using the not operator.

if (!subsystem_is_initialized())
    subsystem_initialize();

For all practical purposes, it's a shorter way to compare a value to zero, with an explicit suggestion that the affected value is boolean. You don't absolutely need it, but then again, neither do you need multiplications (you can loop over an addition), additions (you can do it with binary logic), or most of binary logic (you can do pretty much anything with NANDs, I've been told, but I haven't researched it myself).

Also keep in mind that, like almost all other C++ operators, it can be overloaded by classes.

like image 154
zneak Avatar answered Nov 06 '22 08:11

zneak


A language is not always (in fact as good as never) defined to have the minimal set of features but a set of features that useful. For example, if you had the following code:

if (!test_something())
  do_something();

You could also express this as

if (test_something()) {
} else
  do_something();

but it would be less easy to read. So, while logical negation can usually be expressed by other constructs of the C++ language, it helps readability to express negation explicitly to indicate your intent.

like image 45
bitmask Avatar answered Nov 06 '22 10:11

bitmask