Boolean Algebra

Two Boolean values: true and false

Used to control flow of control in programs


C has no Boolean data type--we substitute "boolean-valued" integer variables.


Two kinds of operators in C return boolean values:

1. Arithmetic:

Relational operators
< less than
<= less than or equal to
== equal to
>= greater than or equal to
> greater than
!= not equal

Example

int x, y;
...            /* initialize x and y somehow */
The expression x < y is a boolean expression; it returns true if the value stored in x is less than the value stored in y.

The operands for relational operands are typically numeric (int or float), but we also use them to compare characters.


Two kinds of operators in C return boolean values:

2. Logical:

Logical operators
&& AND
|| OR
! NOT

Logical operators take boolean operands and return a boolean result.

&& and || take two operands

! takes one operand


The operands for logical operators can be:


Examples:

x > y && y > z /* x and y are numeric types */

x && y /* x and y are boolean-valued variables */

(x && y) || !z /* x, y and z are boolean-valued variables */


Truth tables for logical operators:

x y x && y
f f f
f t f
t f f
t t t
x y x || y
f f f
f t t
t f t
t t t
x !x
f t
t f

Simplification of Boolean Expressions

Simplification requires the use of equivalences:

  1. !(x = y) Û x != y
  2. !(x < y) Û x >= y
  3. !(x <= y) Û x > y
  4. !(x > y) Û x <= y
  5. !(x >= y) Û x < y
  6. !(x && y) Û ! x || ! y
  7. !(x || y) Û ! x && ! y
  8. !(! x) Û x

Example:

!((!x && y) && (x || !y) 

Û !(!x && y) || ! (x || !y) (by g)

Û !(!x) || !y || !(x || !y) (by f)

Û !(!x) || !y || !x && !(!y) (by g)

Û x || !y || !x && y (by h, applied twice)