# Boolean logic

In the previous section, we can combined **gates** to solve larger problems with yes/no questions.

## Logic in software

When programming, we will often have to deal with decisions on a logical level.
To allow us to do that, all programming languages offer some kind of boolean logic operators.

Recap: Boolean values

We have established that in digital logic, we work with two values. They represent the concept of “true” and “false”.

Their meanings can be different depending on how you look at them, for instance:

true |
false |

yes |
no |

1 |
0 |

on |
off |

positive voltage |
negative voltage |

These values are also referred to as a **boolean value** or **boolean** for short. A boolean is something that can have one of two values and nothing else.

For this chapter, we will use `true`

and `false`

to refer to our boolean values.

## The operators

Let’s see what those operators are:

- Not (negation)
- And (conjunction)
- Or (disjunction)
- Exclusive Or (XOR)

Let’s dive into each of them!

## NOT

This operator is most useful in combination with one of the other two operators or alone.
It inverts, i.e. “turns around” a boolean value:

x |
NOT x |

true |
false |

false |
true |

In most programming languages this operator is either expressed as `!`

(e.g. `!x`

is the negation of a boolean `x`

) or the word `not`

.

Example

```
var child = { wasNice: false };
var wasNice = child.wasNice;
var wasNaughty = !wasNice; // wasNaughty will be true
```

## AND

The logical *and* operator outputs true when the two input booleans **are both** true.

x |
y |
x AND y |

false |
false |
false |

true |
false |
false |

false |
true |
false |

true |
true |
true |

Example

```
var user = { isLoggedIn: true, isAdmin: false };
if (user.isLoggedIn && user.isAdmin) {
console.log('Welcome, boss!');
}
```

In most programming languages this operator is either expressed as `&&`

(e.g. `x && y`

) or the word `and`

.

## OR

The logical *or* operator outputs true when **at least one** of two input booleans is true.

x |
y |
x OR y |

false |
false |
false |

true |
false |
true |

false |
true |
true |

true |
true |
true |

In most programming languages this operator is either expressed as `||`

(e.g. `x || y`

) or the word `or`

.

Example

```
var wantsKetchup = true;
var wantsMayo = false;
if (wantsKetchup || wantsMayo) {
console.log('Putting sauce(s) on your fries...');
}
```

## XOR

The `OR`

operator returns `true`

when *at least one* of its inputs is true. What if we want to return `true`

when *exactly* one input is true?

This is a common logical operator called *e***X**clusive **OR** or `XOR`

for short.
It is usually not considered a *basic* boolean operator. It is a combination of `NOT`

, `AND`

and `OR`

.

Creating the XOR from other operators

The `XOR`

operator is a combination of `NOT`

, `AND`

and `OR`

operations.
We have two booleans `x`

and `y`

and we only want to have a `true`

as the result if *either* `x`

or `y`

are `true`

, but *not* if *both* are `true`

.

There are at least two ways of implementing this with logical `NOT`

, `AND`

and `OR`

:

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

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

Here’s a logical circuit to demonstrate this by comparing the first way to an actual XOR:

Here is an interactive version