Printing a number in brainfuck?
Asked Answered
T

2

19

I've searched for a while, but i couldn't find anything that could help me.

Let's say the first cell(or value, etc.) equals 165. How do i print "165"?

My idea was to cut the number into seperate pieces: 1,6 and 5. It would than be no problem to print them.

Note: I don't just want to print "165". I want to print the value the first cell has. No matter if it's 165, 255, 0, 1 or anything else.

Terce answered 24/9, 2012 at 16:49 Comment(0)
F
16

use a famous modulo procedure ( http://esolangs.org/wiki/brainfuck_algorithms will help you)

>+++++++++++[-<+++++++++++++++>] # initialize 165 at first cell
>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-
<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++
<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]
Fioritura answered 19/12, 2012 at 6:15 Comment(2)
+1 for the link. But this function only works for 3 digits numbers.Dietetics
There's code that works for all cell sizes on the same page.Pericope
R
15

I have created a simple language that compiles to brainfuck which can be found here: http://code.google.com/p/brainfuck-compiler. There is a compiler for the language implemented in java.

I use the following function in that language to output a number. Maybe you can analyze the generated code and see if anything can be of use to you from it. One word of warning though, it does generate a bit of redundant copying around of cells (never got around to optimizing this). Anyway here's an example program and the BF code it generates. (note that indentations MUST be tabs in my language)

declare n, 165
declare digits
while n
    push n % 10
    digits = digits + 1
    n = n / 10
if digits
    while digits
        out pop + 48
        digits = digits - 1
else
    outs "0"

And here is the generated code for that:

>>>>>>>>>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++[<+>-]<[>>+>+<<<-]>>>[<<<+>>>-]<[[-]<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-
]++++++++++<[>>+>+<<<-]>>>[<<<+>>>-]<[>+<-]>[<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<[>+
<<-[>>[-]>+<<<-]>>>[<<<+>>>-]<[<-[<<->>[-]]+>-]<-]<<+>]<[>>+<<-]>>[<<<[>+>+<<-]>
>[<<+>>-]>-]<<[<<->>-]<[-]<[>>>>>>>>+<<<<<<<<-]>>>>>>>>>[>>]+[<<]>[>[>>]<+<[<<]>
-]<<<<<<<<<<[>>+>+<<<-]>>>[<<<+>>>-]+[<+>-]<<<[-]>>[<<+>>-]<<<[>>>+>+<<<<-]>>>>[
<<<<+>>>>-]++++++++++<[>>+<<-]>>[<[>>+>+<<<-]>>>[<<<+>>>-]<[>+<<-[>>[-]>+<<<-]>>
>[<<<+>>>-]<[<-[<<<->>>[-]]+>-]<-]<<<+>>]<[-]<<<<[-]>>>[<<<+>>>-]<<<[>>>+>+<<<<-
]>>>>[<<<<+>>>>-]<[<+>-]<]<[>+>+<<-]>>[<<+>>-]<[>+<[-]]+>[<[-]<[>>>+>+<<<<-]>>>>
[<<<<+>>>>-]<[[-]>>>>>>>>[>>]<[<[<<]<<<<<+>>>>>>>[>>]<-]<-<<[<<]<<<<<>++++++++++
++++++++++++++++++++++++++++++++++++++[<+>-]<.[-]<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+
>>>>>-]+[<->-]<<<<<[-]>>>>[<<<<+>>>>-]<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-]<[<+
>-]<]<[-]]<[>>++++++[<++++++++>-]<.[-]<[-]]<[-]<[-]>>>>>>>>>>>>[>[-]>]<<[-<<]<<<
<<<<<<<<<<<<<<[-]<[-]
Riddell answered 27/11, 2012 at 11:49 Comment(2)
Did you ever migrate the project to GitHub?Chaos
Not really. Long since abandoned. I had made a bet with a friend of mine to somehow make a game in brainfuck. I made this compiler, wrote the game, won the bet and pretty much abandoned it.Riddell

© 2022 - 2024 — McMap. All rights reserved.