Logical NOT Operator

The logical NOT operator is '!'

The result of the logical NOT operator is 0 if its argument is logical TRUE and it is 1 if its argument is logical FALSE

logical TRUE is defined as any non-zero value
logical FALSE is defined as the value zero

Put this another way, the result of the logical NOT operator is 1 if its argument is 0 and it is 0 if its argument is anything else (non-zero)

If we call the input A and the output B we can show the logical NOT function as:

  A   B
 !  FALSE  ->  TRUE
 !  TRUE  ->  FALSE
The binary NOT function operates on all bits in parallel (see binary NOT for more details)
  A   B
 ~  0 -> 1
 ~  1 -> 0
Logical NOT and binary NOT differ subtlely.

With binary NOT, 0x02 is logical TRUE and its complement (the result of applying the binary not function) is 0xFD which is also logical TRUE. However the logical NOT of 0x02 (the result of applying the logical not function) is 0 which is logical FALSE

Detailed Truth Tables

binary

  logical

  logical

  A   B           A   B           A   B
 ~  1 -> 0xFE          !  1 -> 0          !  TRUE -> FALSE
 ~  0 -> 0xFF          !  0 -> 1          !  FALSE -> TRUE
binary

  logical

  logical

  A   B           A   B           A   B
 ~  0xFF -> 0          !  0xFF -> 0          !  TRUE -> FALSE
 ~  0 -> 0xFF          !  0 -> 1          !  FALSE -> TRUE

Common use of logical NOT operator

The logical NOT is normally used in conditional expressions such as if and while statements

e.g.


	if  X >= 0  &&  X <= 9  then
		// execute these statements if X is between 0 and 9 inclusive
	endif


	if  ! (X >= 0  &&  X <= 9)  then
		// execute these statements if X is NOT between 0 and 9 inclusive
	endif