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:

[->>+<[->-]>[<<[-]>>->]<<<]>[[-]>+<]<
Open in the playground37 characters

What it needs

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
Table 1. The cost as a function of the values.
FormPlain stepsOptimised steps
Counting both downabout 16 per unit of the smaller numberabout 8 per unit of the smaller number
Greater than, with balanced loops onlygrows with x × ygrows with x × y
By bits, on wrapping cells of a known widththousands: the shifts are long loops35 to 50, whatever the numbers
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Counting both downx = 3; y = 58376130
Counting both downx = 5; y = 38377435
Counting both downx = 200; y = 2508373,3091,606
Greater than, with balanced loops onlyx = 5; y = 38404,0061,080
Greater than, with balanced loops onlyx = 200; y = 250840454,901121,801
By bits, on wrapping cells of a known widthx = 3; y = 53212,709more than 30,000,00037
By bits, on wrapping cells of a known widthx = 200; y = 2503212,709more than 30,000,00047

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.

[>>+<[->[-]>+<<]>[->>+<<]>[-<<+>>]<<-<-]
Open in the playground40 characters
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.

[->>>-<<<]>[->>+<<]>>[->+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<<]>>>>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<<<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>>>]<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]<<<<[->+>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[[-]>>>[-<<<<->>>>>+<]>[-<+>]>>>+<<<<<<<]<[-<+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[[-]>>>[-<<<<<-->>>>>>+<]>[-<+>]>>>++<<<<<<<]<<[->+>++++++++++++++++++++++++++++++++<<]>>[[-]>>>[-<<<<---->>>>>+<]>[-<+>]>>>++++<<<<<<<]<[-<+>>++++++++++++++++<]>[[-]>>>[-<<<<<-------->>>>>>+<]>[-<+>]>>>++++++++<<<<<<<]<<[->+>++++++++<<]>>[[-]>>>[-<<<<---------------->>>>>+<]>[-<+>]>>>++++++++++++++++<<<<<<<]<[-<+>>++++<]>[[-]>>>[-<<<<<-------------------------------->>>>>>+<]>[-<+>]>>>++++++++++++++++++++++++++++++++<<<<<<<]<<[->+>++<<]>>[[-]>>>[-<<<<---------------------------------------------------------------->>>>>+<]>[-<+>]>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<<<<]<[->+<]>[[-]>>>>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<<<<]>>>[-]>>>>[-<<<<<<<<<<->>>>>>>>>+>]<[->+<]<<<<<<<<<[>>>>>>>>>>[-]<<<<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>]<<[[-]>>>[-<<<<->>>>>+<]>[-<+>]<<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>]<<[[-]>>>[-<<<<<<-->>>>>>>+<]>[-<+>]<<<<]<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++++++++++++++++++++++++++++++++>>]<<[[-]>>>[-<<<<---->>>>>+<]>[-<+>]<<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++++++++++++++++>>]<<[[-]>>>[-<<<<<<-------->>>>>>>+<]>[-<+>]<<<<]<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++++++++>>]<<[[-]>>>[-<<<<---------------->>>>>+<]>[-<+>]<<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++++>>]<<[[-]>>>[-<<<<<<-------------------------------->>>>>>>+<]>[-<+>]<<<<]<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<++>>]<<[[-]>>>[-<<<<---------------------------------------------------------------->>>>>+<]>[-<+>]<<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[-<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>]<[[-]>>>[-<<<<<<-------------------------------------------------------------------------------------------------------------------------------->>>>>>>+<]>[-<+>]<<<<]>>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]<<<<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[-<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>]<[[-]>>>>[-<<<<<->>>>+>]<[->+<]<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[-<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>]<[[-]>>>>[-<<<<<<<-->>>>>>+>]<[->+<]<<<]<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[-<++++++++++++++++++++++++++++++++>]<[[-]>>>>[-<<<<<---->>>>+>]<[->+<]<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[-<++++++++++++++++>]<[[-]>>>>[-<<<<<<<-------->>>>>>+>]<[->+<]<<<]<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[-<++++++++>]<[[-]>>>>[-<<<<<---------------->>>>+>]<[->+<]<<<]<[-<<+>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[-<++++>]<[[-]>>>>[-<<<<<<<-------------------------------->>>>>>+>]<[->+<]<<<]<<<[->>+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<]>>>>[-<++>]<[[-]>>>>[-<<<<<---------------------------------------------------------------->>>>+>]<[->+<]<<<]<[-<<+>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[[-]>>>>[-<<<<<<<-------------------------------------------------------------------------------------------------------------------------------->>>>>>+>]<[->+<]<<<]>>>>[-<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>]<<<<<<<[->>+>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<]>>>[[-]>>>[-<<<<->>>>>+<]>[-<+>]<<<<]<[-<<+>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[[-]>>>[-<<<<<<-->>>>>>>+<]>[-<+>]<<<<]<<<[->>+>++++++++++++++++++++++++++++++++<<<]>>>[[-]>>>[-<<<<---->>>>>+<]>[-<+>]<<<<]<[-<<+>>>++++++++++++++++<]>[[-]>>>[-<<<<<<-------->>>>>>>+<]>[-<+>]<<<<]<<<[->>+>++++++++<<<]>>>[[-]>>>[-<<<<---------------->>>>>+<]>[-<+>]<<<<]<[-<<+>>>++++<]>[[-]>>>[-<<<<<<-------------------------------->>>>>>>+<]>[-<+>]<<<<]<<<[->>+>++<<<]>>>[[-]>>>[-<<<<---------------------------------------------------------------->>>>>+<]>[-<+>]<<<<]>>>[-]>>+<<<<<<[[-]>>>>>>-<<<<<<]>>>>>>[->>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]<<<<<<<<]>>>>>>>>>>[<<<<<<<<<<<<+>>>>>>>>>>>>[-]]
Open in the playground12,709 characters
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

Sources

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.