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:

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

What it needs

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
Table 1. The cost as a function of the values. Exact formulas are checked on every case they run.
FormPlain stepsOptimised steps
Not (flag)124
Not (any value)12 + 2x7 when x is not 0, else 4
And (flags)at most 14at most 6
And (any values)grows with x + y (2 per unit)at most 11
Or (flags)at most 16at most 6
Or (any values)grows with x + y (2 per unit)at most 15
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Not (flag)x = 0817124
Not (flag)x = 1817124
Not (any value)x = 7819267
And (flags)x = 0; y = 181663
And (flags)x = 1; y = 1816146
And (any values)x = 3; y = 9820389
Or (flags)x = 0; y = 081643
Or (flags)x = 0; y = 1816116
Or (any values)x = 128; y = 12883254213

Other forms

Not (any value)

x = 1 if x was 0, else 0.

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

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

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

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

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

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.