Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brainfuck compare 2 numbers as greater than or less than

Tags:

brainfuck

How can I compare two numbers with an inequality? (greater than or less than)

I want to compare single digits For example

1 2
5 3
9 2

etc.

like image 507
Krzysztof Lewko Avatar asked May 29 '11 15:05

Krzysztof Lewko


1 Answers

This is the best way to compare two numbers.Why because, if you are intelligent enough, you can use the same code in bigger programs.It's highly portable.

Assume we have two numbers a,b. we have two blocks : if( a>=b ) and else, Hope its enough.

    0 1 0 a b 0

Make the array like this. And point to the (4) i.e. point to the a

    +>+<                   This is for managing if a=0 and b=0
    [->-[>]<<]             This is a magic loop. if a is the one which 
                           reaches 0 first (a<b),then pointer will be at(4).
                           Else it will be at (3)
    <[-  
         //       BLOCK (a>=b)
         //You are at (2) and do whatever you want and come back to (2).
         //Its a must
    ]
    <[-<
         //       BLOCK(a<b)
         //You are at (1) and do whatever you want and come back to (1).
         //Its a must
    ]

It will not affect the following program code as both the code blocks will end up in (1) You can do further coding assuming that pointer will reach (1)

Please remove the documentation if you copy the code. Because code contains some valid brainfuck symbols like < . , etc.

like image 183
Dheeraj Ram Avatar answered Oct 25 '22 02:10

Dheeraj Ram