Code patternsControlLess than
Less than
Sets a flag to 1 when the first number is smaller than the second.
Watch it run
The form “Counting both down” works on 4 cells, named “x”, “y”, “answer” and “zero”. Press Play, drag the step slider forwards and back, or change x and y with the sliders:
The figure needs JavaScript. The program below is the result for the example values.
Brainfuck cannot see which of two numbers is larger. It can only count both down together and see which one runs out first.
Each round takes 1 from x. Then an if and else on y: if y still has something, take 1 from it; if not, x was larger, so clear x to stop.
When the loop ends, y holds what was left: y − x, but never below zero. x was smaller exactly when something is left.
Why this is costly. The loop has a decision inside, so it is not flat, and no optimiser can fold it. The cost grows with the smaller of the two numbers. On 32-bit cells that can be billions of rounds.
The code
This is the program the figure ran. It is brainfuck as it stands: copy it, or open it in the playground, where you can change it:
[->>+<[->-]>[<<[-]>>->]<<<]>[[-]>+<]<
What it needs
- Main form: two zero cells right of y. Both inputs are used up. Any cell size, wrapping or not.
- The numbers are read as unsigned (0 and up).
- The "by bits" form needs wrapping cells of a known width and the project generators.
Common mistakes
- Subtracting and looking at the result. On wrapping cells 3 − 5 is 254: not zero, and not visibly negative either.
- Using the balanced-loops form on large numbers: its cost grows with x × y.
What it costs
A plain step is one command. An optimised step is one step of an interpreter that folds runs of the same command and flat loops. Choose the values and watch the two bars; What costs what explains how cost is counted:
The figure needs JavaScript. The table below has the measured steps.
Cost as a formula, and measured
| Form | Plain steps | Optimised steps |
|---|---|---|
| Counting both down | about 16 per unit of the smaller number | about 8 per unit of the smaller number |
| Greater than, with balanced loops only | grows with x × y | grows with x × y |
| By bits, on wrapping cells of a known width | thousands: the shifts are long loops | 35 to 50, whatever the numbers |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| Counting both down | x = 3; y = 5 | 8 | 37 | 61 | 30 |
| Counting both down | x = 5; y = 3 | 8 | 37 | 74 | 35 |
| Counting both down | x = 200; y = 250 | 8 | 37 | 3,309 | 1,606 |
| Greater than, with balanced loops only | x = 5; y = 3 | 8 | 40 | 4,006 | 1,080 |
| Greater than, with balanced loops only | x = 200; y = 250 | 8 | 40 | 454,901 | 121,801 |
| By bits, on wrapping cells of a known width | x = 3; y = 5 | 32 | 12,709 | more than 30,000,000 | 37 |
| By bits, on wrapping cells of a known width | x = 200; y = 250 | 32 | 12,709 | more than 30,000,000 | 47 |
Other forms
Greater than, with balanced loops only
z = 1 if x is greater than y. Layout: x, y, two scratch cells, z. y ends as y − x. Each round moves all of y away and back.
[>>+<[->[-]>+<<]>[->>+<<]>[-<<+>>]<<-<-]
Run this form step by step
The form “Greater than, with balanced loops only” works on 5 cells; the named ones are “x”, “y”, “scratch” and “z”. Press Play, drag the step slider forwards and back, or change x and y with the sliders:
The figure needs JavaScript. The program below is the result for the example values.
By bits, on wrapping cells of a known width
Works out y − x in one step, then reads it bit by bit and keeps it only if it is positive. The cost does not depend on the numbers. Layout: x, y, answer, then 12 scratch cells.
[->>>-<<<]>[->>+<<]>>[->+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<<]>>>>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<<<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>>>]<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]<<<<[->+>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[[-]>>>[-<<<<->>>>>+<]>[-<+>]>>>+<<<<<<<]<[-<+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[[-]>>>[-<<<<<-->>>>>>+<]>[-<+>]>>>++<<<<<<<]<<[->+>++++++++++++++++++++++++++++++++<<]>>[[-]>>>[-<<<<---->>>>>+<]>[-<+>]>>>++++<<<<<<<]<[-<+>>++++++++++++++++<]>[[-]>>>[-<<<<<-------->>>>>>+<]>[-<+>]>>>++++++++<<<<<<<]<<[->+>++++++++<<]>>[[-]>>>[-<<<<---------------->>>>>+<]>[-<+>]>>>++++++++++++++++<<<<<<<]<[-<+>>++++<]>[[-]>>>[-<<<<<-------------------------------->>>>>>+<]>[-<+>]>>>++++++++++++++++++++++++++++++++<<<<<<<]<<[->+>++<<]>>[[-]>>>[-<<<<---------------------------------------------------------------->>>>>+<]>[-<+>]>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<<<<]<[->+<]>[[-]>>>>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<<<<]>>>[-]>>>>[-<<<<<<<<<<->>>>>>>>>+>]<[->+<]<<<<<<<<<[>>>>>>>>>>[-]<<<<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>]<<[[-]>>>[-<<<<->>>>>+<]>[-<+>]<<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>]<<[[-]>>>[-<<<<<<-->>>>>>>+<]>[-<+>]<<<<]<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++++++++++++++++++++++++++++++++>>]<<[[-]>>>[-<<<<---->>>>>+<]>[-<+>]<<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++++++++++++++++>>]<<[[-]>>>[-<<<<<<-------->>>>>>>+<]>[-<+>]<<<<]<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++++++++>>]<<[[-]>>>[-<<<<---------------->>>>>+<]>[-<+>]<<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++++>>]<<[[-]>>>[-<<<<<<-------------------------------->>>>>>>+<]>[-<+>]<<<<]<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++>>]<<[[-]>>>[-<<<<---------------------------------------------------------------->>>>>+<]>[-<+>]<<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[-<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>]<[[-]>>>[-<<<<<<-------------------------------------------------------------------------------------------------------------------------------->>>>>>>+<]>[-<+>]<<<<]>>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]<<<<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[-<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>]<[[-]>>>>[-<<<<<->>>>+>]<[->+<]<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[-<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>]<[[-]>>>>[-<<<<<<<-->>>>>>+>]<[->+<]<<<]<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[-<++++++++++++++++++++++++++++++++>]<[[-]>>>>[-<<<<<---->>>>+>]<[->+<]<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[-<++++++++++++++++>]<[[-]>>>>[-<<<<<<<-------->>>>>>+>]<[->+<]<<<]<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[-<++++++++>]<[[-]>>>>[-<<<<<---------------->>>>+>]<[->+<]<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[-<++++>]<[[-]>>>>[-<<<<<<<-------------------------------->>>>>>+>]<[->+<]<<<]<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[-<++>]<[[-]>>>>[-<<<<<---------------------------------------------------------------->>>>+>]<[->+<]<<<]<[-<<+>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[[-]>>>>[-<<<<<<<-------------------------------------------------------------------------------------------------------------------------------->>>>>>+>]<[->+<]<<<]>>>>[-<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>]<<<<<<<[->>+>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<]>>>[[-]>>>[-<<<<->>>>>+<]>[-<+>]<<<<]<[-<<+>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[[-]>>>[-<<<<<<-->>>>>>>+<]>[-<+>]<<<<]<<<[->>+>++++++++++++++++++++++++++++++++<<<]>>>[[-]>>>[-<<<<---->>>>>+<]>[-<+>]<<<<]<[-<<+>>>++++++++++++++++<]>[[-]>>>[-<<<<<<-------->>>>>>>+<]>[-<+>]<<<<]<<<[->>+>++++++++<<<]>>>[[-]>>>[-<<<<---------------->>>>>+<]>[-<+>]<<<<]<[-<<+>>>++++<]>[[-]>>>[-<<<<<<-------------------------------->>>>>>>+<]>[-<+>]<<<<]<<<[->>+>++<<<]>>>[[-]>>>[-<<<<---------------------------------------------------------------->>>>>+<]>[-<+>]<<<<]>>>[-]>>+<<<<<<[[-]>>>>>>-<<<<<<]>>>>>>[->>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]<<<<<<<<]>>>>>>>>>>[<<<<<<<<<<<<+>>>>>>>>>>>>[-]]
Run this form step by step
The form “By bits, on wrapping cells of a known width” works on 15 cells; the named ones are “x”, “y”, “answer” and “y − x”. Press Play, drag the step slider forwards and back, or change x and y with the sliders:
The figure needs JavaScript. The program below is the result for the example values.
Where it is used
- Saturating subtract is the same loop without the last line.
- Build toward a chatbot, lesson 4, "Only one question", shows the wall. Build toward a chatbot, lesson 5, "Peeling off bits", shows the way through, which is the form By bits, on wrapping cells of a known width on this page.
- The chat program never compares by counting. It uses shift, round and clamp and pick the largest by a race.
Sources
- Source: Esolang wiki: Brainfuck algorithms: sections "x = x < y" (Ian Kelly) and "z = x > y" (ais523, the balanced-loops form shown as a variation).
- Source: This project: emit.js and patterns.js: the form by bits is requant with a shift of 0.
Every program on this page was run on many inputs, in plain and in optimised mode, and compared with JavaScript. The explanations are written for this page.