C or C++ : Arithmetic operation on boolean

Classification

Overview

Arithmetic operations will not act in the same way on boolean values as they would on integral values, which may lead to unexpected behavior.

Special logical operators are used for operations with boolean variables:

  • logical negation (NOT) - !a;
  • logical AND - a&&b;
  • logical OR - a||b. .

References

  1. A Guide to Undefined Behavior in C and C++, Part 1