---
# System prepended metadata

title: IB Logic gates and truth tables
tags: [IB, Computer Science, M27 Syllabus]

---

# IB Logic gates and truth tables

This is a note for explaining from A1.2.3 to A1.2.5. 

:::info
This is a note for notes of the IB Syllabus starting M27. If you want to see more, you should check the note of the teaching material index here:

https://hackmd.io/@dprieto/David-Index#Computer-Science-New-syllabus-first-examination-2027
:::


:::warning
:construction_worker: :warning:  This is still a work in progress. It has materials but you may not see it yet complete  :warning: :construction_worker: 
:::

## Intro from booleans

For the cohort of M27 this comes from the examples done on python on leap years and how to pass the IB diploma. 

### Leap year

A leap year is a year that instead of 365 days has 366 days. This adjustment is done because the number of a solar year (the time that takes the Earth to do a turn around the Sun) is not exactly a number that we can divide between days (the time the takes to the earth to rotate around itself). 

:::info
**Modulus operand**:

`%` operand in python doesn't do percentages, it finds the integer reminder of a division operation. 

So if we divide 7 over 2, the result would be 3 and the reminder would be 1. So 7%2 -> 1

Odd numbers %2 return 1. Even numbers %2 return 0.

Any number %10 is going to return the last digit of the number. 

This is not explicit in the syllabus but since they asked it (a lot) in the previous syllabus and is very useful, I added to this course.
:::

```python=
#TO DO
```

#### BUT not every 4 years are leap years

With the aproximation of once every 4 years we could manage if the year was exactly 365.25 years. Since it's not the case we need to do another adjustment. Every century (1900, 1800, 2100) we're taking out the leap year of the year ending with 00 so we are more adjusted to the actual solar year. 

So now we need to double check if that's the case. 

```python=
#TO DO
```

#### BUT not every 100 years are not leap years

We even have one more check to do. Every 1000 years (so year 2000, year 3000) to have another adjustment we have a leap year even if we shouldn't. So we have even another adjustment to do. 

```python=
#TO DO
```



### Truth tables

When analyzing a logic gate we can use a truth table. This is a diagram where you write all the posibilities of the input terms and you write the  final outcome after they happen the logic gates. 

1 means "true" and 0 means "false". 

If we have 1 variable is easy because 


### Karnaugh maps

![Karnaugh.svg](https://hackmd.io/_uploads/SJZhOfOnlg.png)

https://en.wikipedia.org/wiki/Karnaugh_map


## Reference for logic gates

![image-88](https://hackmd.io/_uploads/r1vBhyYFkl.png)

https://www.circuitbasics.com/what-is-digital-logic/

## Explanation of logic gates from github

Reference: https://github.com/jfarouq/Paper1

---
title: Paper 1 Lesson 2.9 - Boolean statements and logic
---

### Boolean Statements and Logic

As we have seen in earlier notes, a **boolean statement** is a statement that evaluates to either "True" or "False". 

For each of the following boolean statements, decide if it evaluates to true or false.

1. 2 + 2 = 7
2. √49=7 
3. It is raining outside right now.
4. It is not raining outside right now.
5. Mr. Farouq is a teacher and Mr. Farouq is American.
6. Mr. Farouq is a teacher or Mr. Farouq is an American.
7. Either Mr. Farouq is a teacher or Mr. Farouq is an American.

## Boolean operators

There are 6 boolean operators we need to know. Below they are explained.

### NOT

The NOT operator changes a true boolean statement to a false one, and vice versa. So if `A` is true, then `NOT A` is false. The programming equivalent in Typescript and Java is the `!` operator, as in `! isBlue`.

You can represent the behavior of boolean operators with a *truth table* - the one for NOT is simple:

| A   | NOT A |
| --- | ----- |
| 0   | 1     |
| 1   | 0     |

### OR

The OR operator combines two boolean statements. If EITHER of the statements are true, the entire OR statement is true. So `2 = 2 OR 2 = 3` is a true statement. The Typescript / Java equivalent is the `||` operator, as in `x < 0 || y < 0` (which will be true if either value is negative, or both)

For operators like OR that combine two statements, we need a larger truth table.

| A   | B   | A OR B |
| --- | --- | ------ |
| 0   | 0   | 0      |
| 0   | 1   | 1      |
| 1   | 0   | 1      |
| 1   | 1   | 1     |

### AND

The AND operator combines two boolean statements and is only true if BOTH of the statements are true, otherwise it is false. So `2 = 2 AND 2 = 3` is a false statement, but `2 = 2 AND 2 > 1` is true. The Typescript/Java equivalent is the `&&` operator, as in `x < 0 && y < 0` (only true if both are less than 0)

| A   | B   | A AND B |
| --- | --- | ------- |
| 0   | 0   | 0       |
| 0   | 1   | 0       |
| 1   | 0   | 0       |
| 1   | 1   | 1       |

### XOR

The XOR operator stands for *exclusive or* and it is used in situations we might use the English phrase "either...or" - meaning it is NOT okay for both. We might say "You can have either a cookie OR a brownie", which is an exclusive or. There is no single XOR operator in java / Typescript, but if you're sure that `a` and `b` are both boolean values, you can "cheat" by realizing that this simply means they are not equal to each other and use `a != b` to work like XOR.

| A   | B   | A XOR B |
| --- | --- | ------- |
| 0   | 0   | 0       |
| 0   | 1   | 1       |
| 1   | 0   | 1       |
| 1   | 1   | 0       |

### NOR

The NOR operator means "NOT OR" and returns the exact opposite of the OR operator. It is only true if BOTH of the operands are FALSE. So "2 = 2 NOR 2 = 3" is false because one of the statements is true. There is no single NOR operator in java/Typescript, but you can build one combining `!` and `||` like `! (x < 0  || y < 0)`. (a little thinking will realize this is the same as saying `x >= 0 && y >= 0` , which is true - there are usually multiple ways to build complex logical expressions.)


| A   | B   | A NOR B |
| --- | --- | ------- |
| 0   | 0   | 1       |
| 0   | 1   | 0       |
| 1   | 0   | 0       |
| 1   | 1   | 0       |

### NAND

The NAND operator means "NOT AND" and returns the opposite of the AND operator. `A NAND B` is logically equivalen to `NOT A OR NOT B` and that is usually more common, but we still have to know the NAND operator!

| A   | B   | A NAND B |
| --- | --- | -------- |
| 0   | 0   | 1        |
| 0   | 1   | 1        |
| 1   | 0   | 1        |
| 1   | 1   | 0        |

## Building Complex Truth Tables

One required skill for the IB exam is to build truth tables (like the ones above) based on these operators. I will work through an example.

> Build a truth table for the statement `(A OR B) OR (B NAND A)`

To build these, we start by figuring out our headings, working from the inside out. This always starts with our individual letters that represent individual statements (in this case A and B) and then we work our way outward to the complete statement, doing little middle statements as need be following the order or operations. (These operators all have the same power, so if parenthese don't make it clear move form left to right).

The first two columns always go "0011" and "0101" to make all four combinations.

| A   | B   | B NAND A | A OR B | (A OR B) OR (B NAND A) |
| --- | --- | -------- | ------ | ---------------------- |
| 0   | 0   |          |        |                        |
| 0   | 1   |          |        |                        |
| 1   | 0   |          |        |                        |
| 1   | 1   |          |        |                        |

We then build from left to right, following the rules for each operator. NAND the opposite of AND so it is False only when both are True. OR is true whenever either is true.

| A   | B   | B NAND A | A OR B | (A OR B) OR (B NAND A) |
| --- | --- | -------- | ------ | ---------------------- |
| 0   | 0   | 1        | 0      |                        |
| 0   | 1   | 1        | 1      |                        |
| 1   | 0   | 1        | 1      |                        |
| 1   | 1   | 0        | 1      |                        |

Then for the final column, we are applying the OR operator to the two columns I just made! OR is true whenever either of the inputs are true, so the output is...

| A   | B   | B NAND A | A OR B | (A OR B) OR (B NAND A) |
| --- | --- | -------- | ------ | ---------------------- |
| 0   | 0   | 1        | 0      | 1                      |
| 0   | 1   | 1        | 1      | 1                      |
| 1   | 0   | 1        | 1      | 1                      |
| 1   | 1   | 0        | 1      | 1                      |

This statement is always true! Oops, that was a lot of work for a rather simple output!

## Order of Operations

Sometimes, the IB will provide you with a logic statement that doesn't include parentheses, so you have to figure out which order to apply your operations. The rules are:

1. NOT goes first, so NOT A OR B is the same as (NOT A) OR B
2. AND and NAND go next (left-to right) so A OR B AND C is the same as A OR (B AND C)
3. OR, XOR, and NOR go last (left to right) so A OR B XOR C is (A OR B) XOR C but A XOR B OR C is (A XOR B) OR C

This not every year happens but when it happens it's a pain. 

Here's a photo of me explaining exactly this before a mock where this happened. 

![image](https://hackmd.io/_uploads/S13hDfd2gl.png)
_natural JoJo's pose_

### Practice problems 

Try these examples (bonus: for each one try to come up with a DIFFERENT statement that would have the same result)

1.  Create a truth table for the statement `(A NOR B) OR A`
  
    <details markdown="1"><summary>Click to expand answer</summary>

    | A   | B   | A NOR B | (A NOR B) OR A |
    | --- | --- | ------- | -------------- |
    | 0   | 0   | 1       | 1              |
    | 0   | 1   | 0       | 0              |
    | 1   | 0   | 0       | 1              |
    | 1   | 1   | 0       | 1              |

    </details>

2.  Create a truth table for the statement `(A XOR B) AND A`
   
     <details markdown="1"><summary>Click to expand answer</summary>

    | A   | B   | A XOR B | (A XOR B) AND A |
    | --- | --- | ------- | --------------- |
    | 0   | 0   | 0       | 0               |
    | 0   | 1   | 1       | 0               |
    | 1   | 0   | 1       | 1               |
    | 1   | 1   | 0       | 0               |

    </details>
    
3.  Create a truth table for the statement `NOT (A XOR B)`

    <details markdown="1"><summary>Click to expand answer</summary>

    | A   | B   | A XOR B | NOT (A XOR B) |
    | --- | --- | ------- | ------------- |
    | 0   | 0   | 0       | 1             |
    | 0   | 1   | 1       | 0             |
    | 1   | 0   | 1       | 0             |
    | 1   | 1   | 0       | 1             |

    </details>

4.  Create a truth table for the statement `NOT (A OR B)`
   
    <details markdown="1"><summary>Click to expand answer</summary>

    | A   | B   | A OR B | NOT (A OR B) |
    | --- | --- | ------ | ------------ |
    | 0   | 0   | 0      | 1            |
    | 0   | 1   | 1      | 0            |
    | 1   | 0   | 1      | 0            |
    | 1   | 1   | 1      | 0            |

    </details>
    
5.  Create a truth table for the statement `NOT A AND NOT B`

    <details markdown="1"><summary>Click to expand answer</summary>

    | A   | B   | NOT A | NOT B | NOT A AND NOT B |
    | --- | --- | ----- | ----- | --------------- |
    | 0   | 0   | 1     | 1     | 1               |
    | 0   | 1   | 1     | 0     | 0               |
    | 1   | 0   | 0     | 1     | 0               |
    | 1   | 1   | 0     | 0     | 0               |
    
    </details>

6.  Create a truth table for the statement `A AND (B NOR C)` - note, with three inputs the table will need 8 rows!

<details markdown="1"><summary>Click to expand answer</summary>

  | A   | B   | C   | B NOR C | A AND (B NOR C) |
  | --- | --- | --- | ------- | --------------- |
  | 0   | 0   | 0   | 1       | 0               |
  | 0   | 0   | 1   | 0       | 0               |
  | 0   | 1   | 0   | 0       | 0               |
  | 0   | 1   | 1   | 0       | 0               |
  | 1   | 0   | 0   | 1       | 1               |
  | 1   | 0   | 1   | 0       | 0               |
  | 1   | 1   | 0   | 0       | 0               |
  | 1   | 1   | 1   | 0       | 0               |
</details>

7.  Create a truth table for the statement `(A XOR C) OR B` - note, with three inputs the table will need 8 rows!

<details markdown="1"><summary>Click to expand answer</summary>

  | A   | B   | C   | A XOR C | (A  XOR C) OR B |
  | --- | --- | --- | ------- | --------------- |
  | 0   | 0   | 0   | 0       | 0               |
  | 0   | 0   | 1   | 1       | 1               |
  | 0   | 1   | 0   | 0       | 1               |
  | 0   | 1   | 1   | 1       | 1               |
  | 1   | 0   | 0   | 1       | 1               |
  | 1   | 0   | 1   | 0       | 0               |
  | 1   | 1   | 0   | 1       | 1               |
  | 1   | 1   | 1   | 0       | 1               |
</details>

8.  Create a truth table for the statement `(A NAND B) XOR NOT C` - note, with three inputs the table will need 8 rows!

<details markdown="1"><summary>Click to expand answer</summary>

  | A   | B   | C   | A NAND B | NOT C | (A  NAND B) XOR NOT B |
  | --- | --- | --- | -------- | ----- | --------------------- |
  | 0   | 0   | 0   | 1        | 1     | 0                     |
  | 0   | 0   | 1   | 1        | 0     | 1                     |
  | 0   | 1   | 0   | 1        | 1     | 0                     |
  | 0   | 1   | 1   | 1        | 0     | 1                     |
  | 1   | 0   | 0   | 1        | 1     | 0                     |
  | 1   | 0   | 1   | 1        | 0     | 1                     |
  | 1   | 1   | 0   | 0        | 1     | 1                     |
  | 1   | 1   | 1   | 0        | 0     | 0                     |
</details>

## Using diagrams.net to represent logic gates

You can add the logic gates shapes into diagrams.net using more shapes on the left bottom

![image](https://hackmd.io/_uploads/B1jg-Df6el.png)


and then you go to "others -> electrical"

![image](https://hackmd.io/_uploads/BybRgwG6xl.png)


And there you have them (with some other diagrams that we don't use)

### Bouncer example 

In a club we have a bouncer with very strict instructions for allowing people into the disco or not. They have to be over the allowed age (`age`), they have to be not drunk (`drunk`) and there is a special dress code (`dressCo`) that only applies if they are male (`male`). So, if they are male, they need to comply with that dresscode, if they are not, they don't have. 

## Transistors and physical logic gates

https://www.youtube.com/watch?v=nB6724G3b3E

{%youtube nB6724G3b3E%}