Version 1 of Boolean Logic

Updated 2003-04-16 15:26:51

Boolean logic is the formal mathematical foundation of the most fundamental computer circuits, as well as a major component in the design of complex digital circuits, and as such is important for every serious programmer to at least have some knowledge about, unless one wants to be bereft of fundamental, relevant and fun concepts, and not be aware of the reason for the efficiency of certain and inefficiency of other programming constructs.

In tcl, it is fun to interactively play around with these fundamantals, to begin with in basic form, later I'll do a page where these principles are applied and shown graphically.

As almost everybody (here) will know, booleans are two valued numbers, meaning they are 1 or 0, or in electronical circuits which carry them, a high or a low signal, for instance 5 Volts or 0 Volts. Like with decimal numbers, we can combine 1's and 0's in a list, like decimal numbers, and define operations on them. Parallel operations as with decimal numbers can be defined, which can like in the decimal case be carried out using distributive and associative and substituation operations and factoring.

The basic operations are invert, and and or, which can be defined in tcl as:

 proc inv a {return ~$a}
 proc and {a b} {return [expr $a && $b]}
 proc or {a b} {return [expr $a || $b]}

We can list the domain and corresponding logical functions values easily in short lists:

 # truth table format:
 #  inputs   --> output
 # msb  lsb
    for {set i 0} {$i < 2} {incr i} {puts "$i [inv $i]"}
 0 1
 1 0

    for {set j 0} {$j < 2} {inc j} { for {set i 0} {$i < 2} {incr i} {puts "$j$i [and $j $i]"}}
 00 0
 01 0
 10 0
 11 1

    for {set j 0} {$j < 2} {inc j} { for {set i 0} {$i < 2} {incr i} {puts "$j$i [or $j $i]"}}
 00 0
 01 1
 10 1
 11 1

We can define a nand as the combination of an inverter and a AND circuit as a nand:

 proc nand {a b} {return [inv [and $a $b]]}

With as truth table:

    for {set j 0} {$j < 2} {inc j} { for {set i 0} {$i < 2} {incr i} {puts "$j$i [nand $j $i]"}}
 00 1
 01 1
 10 1
 11 0

Combining more than one bit in a conceptual 'word' we can distinguish the weight of the bits, and like with decimal numbers place the highest valued bit left, and the one valued bit at the right. Because every bit contributes a doubling of the set of possible numbers the word can take on, we can say the corresponding weight of each bit is pow(2,n), where n is the bit number starting from zero at the right. The highest number thus formed 11111...1 is pow(2,n)-1, taking the lowest to have corresponding decimal value zero, and represented as 0000...0.