# 2021-10-31 - DataTypes: Logical Values

### DataTypes

- Part 1 - Introduction
- Part 2 - Bits
- Part 3 - Logical Values
- Part 4 - Numbers
- Part 5 - Binary Arithmetic
- Part 6 - Negative Numbers

Last time we looked at bits, binary digits, which can be either a 0 or a 1. Now the question is what can we do with these bits? Well the simplest thing we can do is treat them as logical or boolean values (named after George Boole and the system of Boolean algebra he developed). These can be either true or false. Many languages have a bool or boolean type which is used to hold these kinds of values. There’s a simple mapping between logical values and bits as both have only two possible values. Typically true is represented with and 1 and false with a 0 but this is entirely arbitrary. Collections of bits can be used to store a collection of logical values called bit flags or a boolean array.

Logical values are the basis for allowing computers to store state and make decisions. We can use a logical value to keep track of whether or not the oven is on and to perform a certain set of operations, like cooking a turkey, if it is and a different set if it isn’t, like turning it on. If the oven being on was currently false the computer would turn it on and then set the value to true. Now if the computer did the oven check again it would see it was true and cook the turkey. The computer could also have a loop that repeatedly checks to see if the turkey is cooked and once that becomes true it would take the turkey out of the oven and turn it off. These decision making and looping mechanisms are what allow computers to be smart. Without them they would be limited to simply performing the same set of operations every time.

Logical values can also be combined using various logical operators to produce new logical values that can be used for more complex decision making.

## Not

The inverse, complement or not operation changes one logical value to the other. This allows you to perform an operation if a value isn’t true. (Not Hot) would be false if Hot was true.

Input | Output |
---|---|

True | False |

False | True |

## And

The and operation combines multiple inputs and is true only if all the inputs are true. This allows you perform a specific operation only if a variety of conditions are all true. (Hot and Humid) would be true only if Hot and Humid were both true.

Input 1 | Input 2 | Output |
---|---|---|

False | False | False |

False | True | False |

True | False | False |

True | True | True |

## Or

The or operation combines multiple inputs and is true if any of the inputs are true. This allows an operation to be performed if at least one of the conditions is true. (Hot or Humid) would be true if either Hot or Humid was true.

Input 1 | Input 2 | Output |
---|---|---|

False | False | False |

False | True | True |

True | False | True |

True | True | True |

## Xor

The exclusive-or operation is similar to or but is false if multiple inputs are also true. This allows an operation to be performed if only a single condition is true. (Hot xor Humid) would be true if Hot was true or if Humid was true but not if both were true.

Input 1 | Input 2 | Output |
---|---|---|

False | False | False |

False | True | True |

True | False | True |

True | True | False |

Logical values are useful for storing simple state information but to really do stuff with computers we need the ability to store more complex information. Next time we’re going to look at integer values.

## Comments: