Logical Not Operator


Syntax

 !prefix_expression

Notes

Prefix operators group right to left.

For integral operands, the value returned is 1 when prefix_expression evaluates to zero and 0 otherwise.

The truth table for logical not is depicted below.

P !P
0 1
1 0

The logical negation operator applies the above truth table to its operand on a macroscopic level. It functions quite differently from the bitwise complement operator. The operator~ complements each bit in its operand, whereas the logical negation operator merely tests its operand to see if it is zero or not, and returns a value of 1 or 0 accordingly.

Examples

i = !3          // i == 0
i = !i          // i == 1
j = !i          // j == 0

if !j && i              // Evaluates to true because !j == 1 and i == 1
 {Then Do Something;}