Boolean Logic

Boolean algebra

In Boolean algebra you represent the logical values true and false by the numbers 1 and 0 respectively.

>>> true
ans = 1
>>> false
ans = 0

and, or, not

The basic operators in logic are and, or and not, these are written using the symbols ∧, ∨ and ¬ respectively. If p and q are statements that are either true or false, then you get the truth table

p q pq pq ¬p
true true true true false
true false false true false
false true false true true
false false false false true

The operators ∧ and ∨ are binary operators, they are applied to two operands. The operator ¬ is an unary operator, it applies to one operand.

In Octave (and most other programming languages) the operators ∧, ∨ and ¬ are written using the symbols &&, || and !; giving the truth table

p q p && q p || q !p
1 1 1 1 0
1 0 0 1 0
0 1 0 1 1
0 0 0 0 1

In Octave (and most other programming languages) all numbers that are not 0 are thought of as being true.

>>> a=4.5;
>>> b=0;
>>> (a && b) || !b
ans = 1

Note that you can perform logical operations by using arithmetics.

p && q = pq

p || q = p+q-pq

!p = 1-p

Comparison Operators

When doing a comparison in Octave you apply a comparison operator on two numbers and the result is either true or false, represented by 1 or 0.

operation operators operands result
arithmetic operations + - * / ^ numbers a number
logical operations && || ! logical values a logical value
comparisons > >= < <= == != numbers a logical value

The comparison operators are:

operator explanation
> greater than, >
>= greater than or equal to,
< less than, <
<= less than or equal to,
== equal to, =
!= not equal to,
>>> a=1; b=2; c=2; d=3;
>>> a>=b
ans = 0
>>> b>=c
ans = 1
>>> (a < b) && (c!=d)
ans = 1

>>> a < b && c!=d
>>>parse error:

syntax error

>>> a < b && c!=d
^

further info:

Fuzzy Logics: This article was published in Scientific American 1993, A Partly True Story, by Ian Stewart

That is true →      ← That is false

by Malin Christersson under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Sweden License

www.malinc.se