|
1 | | -# Work In Progress |
| 1 | +# OP_LESSTHAN |
2 | 2 |
|
3 | | -:::warning |
4 | | -This page has not yet been written. If you have experience with bitcoin Script and would like to contribute, please do! You can open a PR [on the repository for this website](https://github.com/thunderbiscuit/opcode-explained). |
| 3 | +:::info |
| 4 | +**Opcode number:** 159 |
| 5 | +**Byte representation:** `0x9f` |
| 6 | +**Short description:** Pop the top two items; push 1 if the second is less than the top, 0 otherwise. |
5 | 7 | ::: |
| 8 | + |
| 9 | +`OP_LESSTHAN` compares the top two items on the stack as integers. If the second item is less than the top item, it pushes `1` (`true`) onto the stack. If not, it pushes an empty array (`false`). Both items are removed from the stack after the comparison. |
| 10 | + |
| 11 | +### Operation |
| 12 | + |
| 13 | +1. Take the top item (`a`) and the second item (`b`) from the stack. |
| 14 | +2. Compare the two items: |
| 15 | + - If `b < a`, push `1` (`true`) onto the stack. |
| 16 | + - Otherwise, push an empty array (`false`) onto the stack. |
| 17 | +3. Remove the original items (`a` and `b`) from the stack. |
| 18 | + |
| 19 | +### Notes |
| 20 | + |
| 21 | +- Both items must be valid integers. Bitcoin Script interprets byte arrays up to **4 bytes** as integers. |
| 22 | +- An empty array (`[]`) is treated as `0` when compared. |
| 23 | +- If there are fewer than two items on the stack when `OP_LESSTHAN` is executed, the script will fail. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## Examples |
| 28 | + |
| 29 | +### Example 1: Second item is less than the top item |
| 30 | + |
| 31 | +```shell |
| 32 | +# ASM script |
| 33 | +OP_2 OP_3 OP_LESSTHAN |
| 34 | + |
| 35 | +# Raw script |
| 36 | +52539f |
| 37 | + |
| 38 | +# Stack (before OP_LESSTHAN) |
| 39 | +3 # top |
| 40 | +2 |
| 41 | + |
| 42 | +# Stack (after OP_LESSTHAN) |
| 43 | +1 # true, as 2 < 3 |
| 44 | +``` |
| 45 | + |
| 46 | +### Example 2: Second item is greater than the top item |
| 47 | + |
| 48 | +```shell |
| 49 | +# ASM script |
| 50 | +OP_3 OP_2 OP_LESSTHAN |
| 51 | + |
| 52 | +# Raw script |
| 53 | +53529f |
| 54 | + |
| 55 | +# Stack (before OP_LESSTHAN) |
| 56 | +2 # top |
| 57 | +3 |
| 58 | + |
| 59 | +# Stack (after OP_LESSTHAN) |
| 60 | +[] # false, as 3 is not less than 2 |
| 61 | +``` |
| 62 | + |
| 63 | +### Example 3: Comparing two zeros |
| 64 | + |
| 65 | +```shell |
| 66 | +# ASM script |
| 67 | +OP_0 OP_0 OP_LESSTHAN |
| 68 | + |
| 69 | +# Raw script |
| 70 | +00009f |
| 71 | + |
| 72 | +# Stack (before OP_LESSTHAN) |
| 73 | +[] # top |
| 74 | +[] |
| 75 | + |
| 76 | +# Stack (after OP_LESSTHAN) |
| 77 | +[] # false, as 0 is not less than 0 |
| 78 | +``` |
0 commit comments