> For the complete documentation index, see [llms.txt](https://kevinli.gitbook.io/crafting-computer/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kevinli.gitbook.io/crafting-computer/memory/cascading-not-gates.md).

# Cascading Not Gates

So far we succeeded in creating the ALU, which does all the heavy arithmetic and logic computations of our computer. However, our computer is kind of passive. You or a human operator has to tell the computer exactly the inputs and operations it need to perform. Wouldn't it be cool if we can store our instructions in the computer and let it do the work for us? Can we store the result of our computations and use them later? All of these tasks require our computer to remember things or have a memory.

Memory enables humans like us to learn new knowledge and development relationships. Not only that, memory forms the foundation for our identity, culture, religion, and ideology. The ability to remember stuff is what makes our society tick. In our opinion, memory is also the key that makes computer truly trancending and magical. Just like before, we will look at the simplest case first - storing 1 bit of information.

The simplest way to store 1 bit is a loop containing two cascading not gates:

![Cascading Not Gates (Initial State)](/files/-M6gPR76ZYTMEUBTbZAr)

The `or` gate is just used to combine the inputs from the switch and the second `not` gate. It does serves no other function in the circuit. The above is the initial state when you finished connecting the circuit but haven't turned on the switch. Notice the red wires which mean undetermined state. These wires are undetermined because the switch outputs a `0` but we don't know what the second `not`gate outputs. So the output of the `or` gate and all the wires connected to its output are undertermined. However, once we turn on the switch:

![Cascading Not Gates (On State)](/files/-M6gPwAf086b5It7urY_)

The or gate is no longer confused because one of its input is `1` so the output has to be `1`. The `1` outputed by the `or` gate is negated once by the first `not` gate to become `0` but is negated again by the second `not` gate to return back to `1`. Now if we turn off the switch:

![Cascading Not Gates (Off State)](/files/-M6gTQl1YG4olwU5V1Qq)

Quite unexpected right? The light bulb remains on because one of the inputs of the `or` gate is `1` so whether the switch is on or off doesn't matter. This behavior is both good and bad. The good part is we successfully created our first memory using digital circuits! We are able to remember the on state forever. The bad part is once we store the value, we can never change it. It's like you can only deposit money into a bank account once and it's immediately frozen forever so you can't deposite more or retrieve your money back. This really sucks because the whole point of having a memory is to be able to be able to add new stuff to it or update the stuff it contains. To create a more useful memory, we will need to create a latch. Before we move on, you can actually implement this in Sim:

```
cascading_not_gates input[1] -> [1] =
  let
    output = or output (not (not input))
  in
  output
```

This circuit is unlike any other ones we built previously. Notice that `output` depends on both `input` and `output` itself. Because any value is undetermined initially, `output` will be undertermined in the first run. However, if we set `input` to `1`, `output` will be set to `1` after the first run and stays at `1` forever because of the `or` gate.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://kevinli.gitbook.io/crafting-computer/memory/cascading-not-gates.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
