BitcoinMachine

Module 02

Arithmetic

Bitcoin Script can do basic math on stack items — addition, subtraction, comparison, and more. The inputs and outputs are all CScriptNum: a compact variable-length integer encoding specific to Bitcoin.

0 / 4 sections

What you'll learn

  • Decode and encode CScriptNum integers
  • Apply OP_ADD, OP_SUB, OP_ABS, OP_NEGATE, OP_MIN, OP_MAX
  • Use OP_WITHIN to perform range checks
  • Understand why Script integers are limited to 4 bytes

01

Add and subtract

OP_ADD pops the top two items, adds them, and pushes the result. OP_SUB subtracts the top from the second-to-top (second − top). Both opcodes consume their inputs — nothing is preserved.

OP_ADD

Pops two items, pushes their sum. Here 3 + 4 = 7.

Script
OP_3
OP_4
OP_ADD
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_3Pushes the number 3 onto the stack.
0 stepsReady
OP_SUB

Pops two items, pushes second − top. Here 9 − 4 = 5.

Script
OP_9
OP_4
OP_SUB
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_9Pushes the number 9 onto the stack.
0 stepsReady
Both operands must fit in 4 bytes (≤ 2 147 483 647). Larger inputs throw a ScriptNumError. All arithmetic operands use CScriptNum encoding.
Real Bitcoin

Why 4-byte integers?

Script was designed for compact transaction scripts, not general computation. 4 bytes (±2 billion) is enough for amounts, counters, and indices while keeping the script bytecode small and the consensus rules simple.

02

Shortcuts and signs

Several opcodes modify the top item in-place without needing a second operand.

OP_1ADD

Adds 1 to the top item. Cheaper than pushing OP_1 and calling OP_ADD.

Script
OP_5
OP_1ADD
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_5Pushes the number 5 onto the stack.
0 stepsReady
OP_1SUB

Subtracts 1 from the top item.

Script
OP_5
OP_1SUB
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_5Pushes the number 5 onto the stack.
0 stepsReady
OP_NEGATE

Flips the sign. Positive becomes negative and vice versa.

Script
OP_7
OP_NEGATE
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_7Pushes the number 7 onto the stack.
0 stepsReady
OP_ABS

Returns the absolute value. Negative numbers become positive.

Script
OP_1NEGATE
OP_3
OP_MUL
OP_ABS
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_1NEGATEPushes the number −1 onto the stack.
0 stepsReady

03

Range operations

OP_MIN and OP_MAX return the smaller or larger of two values. OP_WITHIN checks whether a value falls in the range [min, max).

OP_MIN

Pops two items, pushes the smaller. Here min(3, 7) = 3.

Script
OP_3
OP_7
OP_MIN
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_3Pushes the number 3 onto the stack.
0 stepsReady
OP_MAX

Pops two items, pushes the larger. Here max(3, 7) = 7.

Script
OP_3
OP_7
OP_MAX
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_3Pushes the number 3 onto the stack.
0 stepsReady
OP_WITHIN

Pushes 1 if value is in [min, max), else 0. Stack order: value, min, max. Here 1 ≤ 5 < 10 → true.

Script
OP_5
OP_1
OP_10
OP_WITHIN
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_5Pushes the number 5 onto the stack.
0 stepsReady
Try it yourself — what does OP_10 OP_1 OP_10 OP_WITHIN return? The upper bound is exclusive.

Ctrl+Enter to run

Script
OP_10
OP_1
OP_10
OP_WITHIN
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_10Pushes the number 10 onto the stack.
0 stepsReady

04

Your turn

Use arithmetic to hit a specific target.

Challenge

Leave 21 on the stack

Write any script that leaves exactly the number 21 as the sole item on the stack. There are many valid solutions.

Ctrl+Enter to check

← Home