Clearing a cell
Set a value back to 0. In brainfuck the idiom is [-], which takes one away until nothing is left.
●○○ easy · where to see it
The idea
Many languages have no "set to 0" command. They clear a value with a loop that keeps taking one away while the value is not 0.
Read the story
In brainfuck that is [-]. If the cell is already 0, [ skips the loop. Otherwise - runs once per unit, and ] stops the loop when the cell reaches 0.
Step by step in brainfuck
The cell holds 65 and prints A.
Print A, clear, print 0
The cell holds 65 and prints A. [-] runs 65 times and leaves 0. Then 48 plus signs make the character 0.
Expected output
A0
Output
- Inside
[-]the step count rises while nothing is printed.
Clearing upwards works too
----- makes the cell 251, because 0 minus 1 wraps to 255. [+] counts up through 252, 253, 254 and 255 to 0. Wrapping makes both directions work.
Expected output
0
Output
Side by side
Ook! spells every brainfuck command as two words.
The same idiom in Ook!
Ook! spells every brainfuck command as two words. [ is Ook! Ook?, - is Ook! Ook! and ] is Ook? Ook!. The loop is the same loop.
Expected output
0
Output
The kernel stores 0
The kernel has memory with a pointer, like brainfuck’s tape, but it can store any number directly. 0 PTR STORE clears the cell in one step.
Expected output
A0
Output
Deadfish can only reset by accident
Deadfish has no loop and no "set". Its only way back to 0 is its odd reset rule. 4 squared is 16, squared again is 256, and 256 becomes 0.
Expected output
0
Output
Where it appears
- Every brainfuck program that reuses a cell starts with
[-]. - The multiplication loop ends with its counter cleared, the same idea run as a side effect.
- See brainfuck and Deadfish.