true false |
Boolean values Values stored by the computer -------------------- --------------------------------- false 00000000 true 00000001 00000010 00000011 ... |
The association that many (if not all) computer programming languages use is the following:
Boolean values Values stored by the computer -------------------- --------------------------------- false <--------> 00000000 true <--------> 00000001 true <--------> 00000010 true <--------> 00000011 ... ... |
I.e.: the Boolean value false is represented by the binary number 00000000.
The Boolean value true is associated with all non-zero binary numbers
The following Python program:
/home/cs255001/demo/Python/Boolean/boolean.py if 0: print("0: true") else: print("0: false") if 1: print("1: true") else: print("1: false") if 4: print("4: true") else: print("4: false") |
will output:
cheung@lab1a> python boolean.py 0: false 1: true 4: true |
which shows you that Python uses the following Boolean to integer encoding:
Boolean values Values in Python -------------------- --------------------------------- false <--------> 00000000 (= 0) true <--------> 00000001 (= 1) true <--------> 00000010 (= 2) true <--------> 00000011 (= 3) true <--------> 00000100 (= 4) ... ... |
I highlighted the partial association printed by the Python program in red