Stack
A stack is a Last-In-First-Out (LIFO) data structure. Think of it like a stack of plates:
- You add (push) new plates to the top.
- You remove (pop) plates from the top.
Let's visualize this with our Bitcoin Stack component.
Try pushing some values onto the stack and see how they stack up!
Stack is empty
2. Basic Stack Operations
Bitcoin's script uses stack operations to process data. Let's explore two operations as an example:
OP_ADD
: pops the top two values from the stack, adds them, and pushes the result back.
- Push the number 3 onto the stack
- Push the number 4 onto the stack
- Click the
OP_ADD
button
Stack is empty
OP_EQUAL
: compares the top two stack items. If they're equal, it pushes 1
(true), otherwise 0
(false).
- Push the number 5 twice
- Click the
OP_EQUAL
button
Stack is empty
Now try with different numbers!