22

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.

jgillich
  • 71,459
  • 6
  • 57
  • 85
Krzysztof Lewko
  • 982
  • 7
  • 24

6 Answers6

26

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.

Dheeraj Ram
  • 537
  • 6
  • 13
  • It will not affect the following program code as both the code blocks will end up in (1) You can code assuming that pointer will reach (1) – Dheeraj Ram Nov 11 '12 at 01:44
  • 1
    Did you know that you can always edit your own post to add information, just use the "edit" link button at the end of your post. I also suggest that you have a look to our FAQ : http://stackoverflow.com/faq :) – ForceMagic Nov 11 '12 at 02:04
  • In your "magic loop", the `[>]` is actually an infinite loop. It should be `[>[-]]` to terminate. – Timtech Mar 08 '14 at 00:15
  • 2
    @Timtech Not it's not. It's the code that makes this work. If `b` is zero `[>]` will terminate and then `<<` will end up on the cell before `a` which is zero. If b is non zero it will end up at the zero next to it and the loop will continue until either `a` or `b` are zero. – Sylwester Aug 02 '14 at 00:38
  • Yes, it works. I used the same code for finding sum of two multi-digit numbers.. like I said first, you can reuse this code. Check it out.. http://stackoverflow.com/a/19412942 – Dheeraj Ram Aug 09 '14 at 02:31
  • Does `+>+<` still work if your brainfuck interpreter overflows 255 back to 0? – Mateen Ulhaq Mar 24 '19 at 08:13
  • No, it doesn't work for overflow: 255 (or max cell value) in `a` will always end up in `BLOCK(a – Madjosz May 12 '23 at 10:20
10

Once you know which is the distance between the two numbers you should or decrement both of them in the same loop iteration and then check both for being zero: you will understand which one is the smaller.

Eg:

+++++ > +++ < [->-< check is first is zero, then second]

(this is just to give you a hint, you will have to take care about equal numbers and similar issues.

Jack
  • 131,802
  • 30
  • 241
  • 343
4

I was thinking about this too, and while I'm sure this isn't the best solution, at least it can answer the question of which number is larger =)

The program asks for two characters, outputs '<' if the first is smaller, '>' if it is larger, and '=' if they are equal. After outputting one char, the program halts by asking for additional input.

+>,>,<<[>-[>>>]<[>>-[>++++++++++[->++++++<]>.,]++++++++++[->++++++<]>+.,]<-[>>>]<<[>>>++++++++++[->++++++<]>++.,]<<<]

Hopefully somewhat clearer:

+                                   init (0) to 1
>,                                  read (1)
>,                                  read (2)
<<[                                 loop forever
  >-[>>>]                           decrement (1) going to (4) if (1) != 0
  <[                                goto (0) == 1 if (1) reached 0 (otherwise goto (3))
    >>-[>++++++++++[->++++++<]>.,]  decrement (2) printing lessthan if larger than 0
    ++++++++++[->++++++<]>+.,       if (2) == 0 print '='
  ]
  <-[>>>]                           decrement (2) going to (5) if (2) != 0
  <<[                               goto (0) == 1 if (2) reached 0 (otherwise goto (3))
    >>>++++++++++[->++++++<]>++.,   print largerthan since (2) reached 0 first
  ]
  <<<                               goto(0)
]
1

I made a solution, that gives you back a boolean and the pointer always at the same point.

This is how it looks like at the beginning:

0 0 0 a b 0 0
      p

And these are the two possible outputs:

0 0 0 0 0 1 0   #true
            p

0 0 0 0 0 0 0   #false
            p

The code:

 >>>>
    [                 # while cell != 0
      -               #   decrement a
      [               #   if a != 0
        >-            #     decrement b 
        [             #     if b != 0
          <           #       go left
          <-<         #       undo the finally-block;
        ]             #     finally-block
        <[-]>         #       clear a
        >+>           #       res = 1; move to end-position
        <<<           #       undo the finally-block
      ]               #   finally-block
      >[-]>>          #     clear b; res = 0; move to end-position
    ]                 #

minified version:

 >>>>[-[>-[< <-<]<[-]>>+><<<]>[-]>>]
Atr0x
  • 176
  • 7
-2

Given two numbers A and B, the following code will print A if A is greater than B, B if B is greater than A and C if both are equal.

>>>>>>>>>++++++[>+++++++++++<-]>[>+>+>+<<<-]>+>-> <<<<<<<<<<<,>,< [->-<[>]<<]>>>[>>]>>>>>>>>.

-13

No such thing exists in BF. The > and < in BF move the pointer to the right and to the left, respectively.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151