SantoshGupta7
SantoshGupta7

Reputation: 6197

What's the point of NOT-operator logic?

What's the point of not (!) logic? It seems that you can do everything not can do with all the other logical operators. Is there something that not can do that I am missing?

Upvotes: 1

Views: 356

Answers (2)

Mike Kinghan
Mike Kinghan

Reputation: 61417

You won't deny that the NOT-operator is very convenient in a programming language even if the other operators and built-in constants available in that language render it strictly redundant. Convenience is an adequate justification - in fact it is the justification - for almost all features of all general programming languages. If we didn't care about convenience - which in programming, means productivity - we could write all programs with a set of Turing-complete op-codes far smaller even than any assembly language.

The degree of inconvenience you would face in doing without the NOT-operator depends on the programming language you are considering and specifically on the other operators and built-in-constants that the language provides and their semantics.

In C, for example, the equality operator == exists but there are no built-in constants representing truth and falsity: any integral value all of whose bits are 0 behaves as falsity in boolean operations and all other integral values behave as truth. !cond evaluates to 0 if cond evaluates non-zero and otherwise evaluates to 1. Thus to say that cond is not true without coding !cond you have to code cond == 0, taking at least 2 keystrokes more.

Like C, C++ has equality and inequality operators but unlike C it represents the boolean truth valiues by the built-in constants true and false. Thus to say that cond is not true in C++ without coding !cond you must code either cond != true or cond == false, taking at least 5 keystrokes more.

And the cost of doing without the NOT-operator can potentially compound beyond minor inconvenience. Which of the following can you understand first?:

!(p && !q) == (!p || q)

or:

(((p && (q == 0)) == 0) == ((p == 0) || q)

Upvotes: 2

Jacob Davis-Hansson
Jacob Davis-Hansson

Reputation: 2663

You can implement all logical operators solely with the NAND operator. The NOT operator is for convenience, just like all the others are. In fact, computer systems are implemented solely with either the NAND or the NOR operator. All other operators are abstractions put in place for convenience.

It is convenient, however. Since you mention the "!" operator, I assume you mean boolean operators in general programming languages. Then the not operator is very convenient. Imagine you wanted to express something like "print all names except 'Bob'". You could do that with the != operator, which is a further short-form of !(expression1 == expression2):

if( !(name == 'Bob') ) {
    print name
}

Upvotes: 0

Related Questions