Code patternsControlNot, and, or on flags
Not, and, or on flags
Combines yes-or-no values, where 0 means no and 1 means yes.
Watch it run
The form “Not (flag)” works on 2 cells, named “x” and “scratch”. Press Play, drag the step slider forwards and back, or change x with the slider:
The figure needs JavaScript. The program below is the result for the example values.
Not: start a helper at 1 and take x off it. And: only if x is set does y get moved into the answer. Or: add the two and turn "anything but zero" into 1.
The short forms trust that the cells hold 0 or 1 only. The "any value" forms first turn every non-zero value into a clean 1; they cost a little more.
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
- Short forms: the inputs must be 0 or 1.
- One scratch cell (not) or an answer cell that is zero (and).
- Any cell size. The inputs are used up.
Common mistakes
- Feeding a number such as 2 to a short form: "not 2" gives 255 on 8-bit cells, not 0.
- The short "or" adds the two inputs. With arbitrary numbers the sum can wrap to exactly zero (128 + 128 on 8-bit cells) and the answer is wrong. Use the "any value" form.
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 |
|---|---|---|
| Not (flag) | 12 | 4 |
| Not (any value) | 12 + 2x | 7 when x is not 0, else 4 |
| And (flags) | at most 14 | at most 6 |
| And (any values) | grows with x + y (2 per unit) | at most 11 |
| Or (flags) | at most 16 | at most 6 |
| Or (any values) | grows with x + y (2 per unit) | at most 15 |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| Not (flag) | x = 0 | 8 | 17 | 12 | 4 |
| Not (flag) | x = 1 | 8 | 17 | 12 | 4 |
| Not (any value) | x = 7 | 8 | 19 | 26 | 7 |
| And (flags) | x = 0; y = 1 | 8 | 16 | 6 | 3 |
| And (flags) | x = 1; y = 1 | 8 | 16 | 14 | 6 |
| And (any values) | x = 3; y = 9 | 8 | 20 | 38 | 9 |
| Or (flags) | x = 0; y = 0 | 8 | 16 | 4 | 3 |
| Or (flags) | x = 0; y = 1 | 8 | 16 | 11 | 6 |
| Or (any values) | x = 128; y = 128 | 8 | 32 | 542 | 13 |
Other forms
Not (any value)
x = 1 if x was 0, else 0.
>+<[[-]>-<]>[-<+>]<
Run this form step by step
The form “Not (any value)” works on 2 cells, named “x” and “scratch”. Press Play, drag the step slider forwards and back, or change x with the slider:
The figure needs JavaScript. The program below is the result for the example values.
And (flags)
z = x and y. Layout: x, y, z.
[>[->+<]<-]>[-]<
Run this form step by step
The form “And (flags)” works on 3 cells, named “x”, “y” 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.
And (any values)
z = 1 if both are not 0.
[>[[-]>+<]<[-]]>[-]<
Run this form step by step
The form “And (any values)” works on 3 cells, named “x”, “y” 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.
Or (flags)
x = x or y. Layout: x, y.
[->+<]>[[-]<+>]<
Run this form step by step
The form “Or (flags)” 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.
Or (any values)
x = 1 if either is not 0. Layout: x, y, scratch.
[[-]>>+<<]>[[-]>+<]>[[-]<<+>>]<<
Run this form step by step
The form “Or (any values)” works on 3 cells, named “x”, “y” 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.
Where it is used
- Equal is "not (x − y)".
- Combining tests in the challenges.
Sources
- Source: Esolang wiki: Brainfuck algorithms: sections "not x", "x and y", "x or y" (boolean, logical); the snippets here are our own, shorter forms for the same layouts.
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.