Code patternsControlEqual
Equal
Sets a flag to 1 when two cells hold the same number.
Watch it run
The form “Using up both” works on 2 cells, named “x” and “y”. 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.
Two numbers are equal when their difference is zero. Subtracting is cheap (a flat loop), and "is it zero" is the one question brainfuck can ask.
So: x becomes x − y, and then a "not" (see not, and, or on flags) turns "zero" into 1 and everything else into 0.
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
- Wrapping cells (or x at least y), because x − y may go below zero on the way.
- Short form: no scratch cell; both inputs are used up. Keeping form: two more cells.
- The pointer ends where it started.
Common mistakes
- Thinking the same trick gives "less than". The difference tells you whether the numbers differ, but on wrapping cells not which one is larger: see less than.
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 |
|---|---|---|
| Using up both | grows with x, and with the difference | 4 when equal, else 7 |
| Keeping both | grows with x + y (about 20 per unit) | 8 when equal, else 11 |
| Not equal | grows with x, and with the difference | 3 when equal, else 6 |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| Using up both | x = 7; y = 7 | 8 | 17 | 40 | 4 |
| Using up both | x = 7; y = 9 | 8 | 17 | 49 | 7 |
| Using up both | x = 200; y = 3 | 8 | 17 | 1,128 | 7 |
| Keeping both | x = 7; y = 7 | 8 | 65 | 262 | 8 |
| Not equal | x = 7; y = 9 | 8 | 16 | 48 | 6 |
Other forms
Keeping both
Layout: x, y, answer, scratch.
[->>+>+<<<]>>>[-<<<+>>>]<<[->->+<<]>>[-<<+>>]+<[[-]>-<]>[-<+>]<<<
Run this form step by step
The form “Keeping both” works on 4 cells, named “x”, “y”, “answer” and “scratch”. 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.
Not equal
x = 1 if the two differ.
[->-<]>[[-]<+>]<
Run this form step by step
The form “Not equal” works on 2 cells, named “x” and “y”. 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
- Compare two strings does this once per letter.
- The form Until a newline of while is an equality test against 10.
Sources
- Source: Esolang wiki: Brainfuck algorithms: sections "x = x == y" and "x = x != y" (Jeffry Johnston; the short forms).
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.