Last active
November 22, 2025 17:36
-
-
Save avih/3c7733e251e7edaf161f7f71c840ce69 to your computer and use it in GitHub Desktop.
infinite decimal counter in brainf*ck
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //[ | |
| // infinite decimal counter in bf / by avih | |
| // with tips and improvements from ais523 and int-e | |
| // also: bonus version by int-e at the bottom | |
| //] | |
| ++++++++++[>>>+[>----------[++++++++++[<-]<[->+<<]>]<[->+>>+ | |
| <]>]>+[>>>]<<<[<<++++++++[>+++>++++++<<-]>>-.+<[->--<]<<]<.] | |
| //[ (UNREACHABLE) COMMENTED VERSION | |
| // | |
| // cell value 1 to 10 represent '0' to '9' / have two temps left to each digit | |
| // mem: '\n' 0 0 0 LSD ... 0 0 D ... 0 0 MSD (digits at 4/7/10/etc) | |
| // the temps start as 0 and end as 0 before moving to another digit | |
| // | |
| // comments above/below the code are overview | |
| // comments to the right are memory state after the line | |
| // | |
| // notation: | |
| // digit refers to the location | |
| // val the digit cell value we are processing | |
| // D ascii val derived from the cell value | |
| // '2' specific ascii char (in this case '2' ie ascii 50) | |
| // (==X) condition: current cell value is X | |
| // =Y set current cell to value Y | |
| // ADDR: head is at absolute ADDR | |
| // A @B *C consecutive mem values A B C (typically tmp tmp digit) | |
| // @ is the head position | |
| // * is the digit position we're working on | |
| //] | |
| +++++ +++++ // 0: @10 ie '\n' | |
| [ // 0: @'\n' | |
| >> // 2: '\n' 0 @0 0 *LSD(if any ie maybe 0) | |
| // for(;;) if (==10) {=1; right 3} else {if (==0) {=1}; break} | |
| // ie while (D=='9') {D='0'; head to next digit (more significant)} | |
| // once stopped on non '9': if no digit (past most significant) set as '0' | |
| // (the code is with the if/else clauses order reversed) | |
| >+ // 0 @1 *val(maybe 0) | |
| [>----- ----- // 0 1 *@(val(0 to 10) minus 10) | |
| [ // enter if val was not 10 (ie not '9') | |
| +++++ +++++ // 0 1 *@val(0 to 9) | |
| [<-]<[->+<<]> // 2 'if's / only one entered / implement: | |
| // if (==0) {=1); and break regardless | |
| // 0 @0 *val(1 to 9 ie '0' to '8') | |
| ] | |
| // Heisenberg: we're in one of 2 places (like between the 2 'if's above): | |
| // if val was NOT 10: 0 @0 *val(1 to 9) | |
| // else (val WAS 10): 0 1 *@0 | |
| < | |
| [-> // 0 0 *@0 (val was 10 and now it's 0) | |
| + // 0 0 *@1 (make it 1 ie '0') | |
| >>+ // 0 0 *1 0 @1 nextVal (set continue) | |
| <] | |
| // we know where we are relative to SOME digit but not to which one: | |
| // generally it's: @0 isCont val | |
| // and specifically (with aligned addresses and original *): | |
| // if the original val was NOT 10: @0 0 *val(1 to 9) | |
| // else (was 10 ie '9') 0 0 *1 @0 1 nextVal | |
| >]> | |
| // end: 0 0 *@val(1 to 9 ie '0' to '8') | |
| // inc the digit by 1 | |
| + | |
| // skip the rest and back one (end on the most significant digit) | |
| [>>>]<<< // 0 0 *@MSD(2 to 10 ie '1' to '9') | |
| // while (not 0) {print val plus 47; head to prev digit (less signific)} | |
| [ | |
| <<+++++ +++[>+++>+++++ +<<-] // @0 24 *(val plus 48) | |
| >>-. // 0 24 *@(val plus 47) and print | |
| +< // 0 @24 *(val plus 48) | |
| [->--<] // 0 @0 *val | |
| << // @prevVal | |
| ] // final: '\n' @0 | |
| < . // 0: print ('\n') | |
| ] | |
| //[ (UNREACHABLE) bonus version by int-e: ] | |
| >>+[[----------[+++++++++++[>>]>+<]+>>]<[-<-<<--<]>++[> | |
| ++++++++[<<++>++++++>-]<-.+<[>---<-]<]++++++++++.[-]>>] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment