Is there a way to print out the value at the pointer an a number in brainfuck?
Asked Answered
D

1

1

So, i was wondering how could i print this value as a number NOT as a character an example would be that the pointer is on (125)

(0)(0)(125)(0)(0)

In this case when this snippet of would start, the value at the pointer will be printed out as 125.

I'm hoping that this will work for all numbers from 0 to 255 (cus i olny use 8 bitcells) ,and also pleas emake this use the least ammount of cells (max 13) and commentated well enough, 'cus idk a lot about brainfuck. (also how the hell do u divide??)

Disembogue answered 22/2, 2023 at 18:38 Comment(2)
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.Sarah
Does this answer your question? Printing a number in brainfuck?Syllabi
I
0

Here's one way to do this. We divide by 10, several times in a row if necessary, store the remainders, and then adjust them and output them in the reverse order.

We start with 0 0 0 x 0 0 0 0 0 (we may need up to 9 cells). Assume we start with the pointer at the leftmost 0 here.

While doing the first division, the memory layout will be: 0 remainder divisor dividend quotient 0 0 0 0.

Then if the quotient is nonzero, we use it as the next dividend, so during the second division the layout will be "0 1st_remainder 2nd_remainder divisor dividend quotient 0 0 0". We will increase remainders by 1 so we can easily tell them apart from empty space, to know how long the result is. Also: to print 0 properly as "0" we'll set it up as if it were a remainder, and then if x is nonzero we'll clear it out again and compute remainders properly.

About how the division works: the basic principle here is that we have an outer loop executed (dividend) times, and on the nth time through it sets remainder and quotient to the proper values for (n/divisor). Usually this just means incrementing the remainder, and decrementing the divisor because we're using it as a counter. Each tenth time through, we need to zero the remainder and increment the quotient; we use the remainder to refill the divisor counter at the same time; we do this whenever the divisor counter has been zeroed.

>+>>[ set up fake remainder for 0
  <<->>[ x is nonzero; clear fake remainder and start main loop 
    <++++++++++> set divisor to 10
    [<<+>-[>>>>]<[[>+<-]>>>+>]<<-] divide
    <<+>[-]>> increment remainder and clear divisor
  ]<
]<<[
  ->++++++++[<++++++>-] increase by 47
  <.[-]< output digit and clear
]++++++++++.[-] output linefeed and clear
Imperial answered 23/2, 2023 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.