1

I was wondering how I could 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 (because I only use 8 bit cells), and also please make this use the least number of cells (max 13) and commentated well enough, because I don't know a lot about brainfuck (also how the hell do you divide?).

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. Commented Feb 22, 2023 at 20:12
  • Does this answer your question? Printing a number in brainfuck? Commented Jan 12, 2024 at 21:17

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.