5

How can I check if the value in cell #0 is equal to the value in cell #1? I am trying to write code equivalent to:

if(a == b)
{
    //do stuff
}
else
{
    //do something else
}

I have read Brainfuck compare 2 numbers as greater than or less than, and the second answer gave me a general idea of what I'd need to do, but I cannot figure it out. (That solution gives if a < b, else.)

I am thinking I need to do something along the lines of decrementing both values, and if they reach 0 at the same time, then they are true. But I keep getting stuck at the same exit point every time I think about it.

How can I check if two cells are equal in brainfuck?

Community
  • 1
  • 1
Evorlor
  • 7,263
  • 17
  • 70
  • 141
  • Maybe a stupid question but why on earth would anybody ever torture themselves by using brainf*ck? – Maantje May 20 '16 at 20:03
  • 5
    @Maantje Some people enjoy challenges. Why would anybody ever torture themselves by learning to solve the Rubik's cube? – John Coleman May 21 '16 at 02:24

3 Answers3

4

I think I have it, I'm not a brainfuck expert but this question looked interesting. There might be a simpler way to do it, but I went with your method of decrementing values one by one.

In this case, if the two values in cell 0 and 1 are equal jump a ton forward, if they are not equal jump a little forward (second brackets is the not equal case, third brackets is the equal case)

Note that I'm using brainfucks while statements as a ghetto if (cell != 0)

+++++++++++++++++
>
+++++++++++++++++
>+<
[ - < - >] <[>>>>>] >> [>>>>>>>>>>>>>>>>>>>>>]

Try it online: http://fatiherikli.github.io/brainfuck-visualizer/#KysrKysrKysrKysrKysrKysKPgorKysrKysrKysrKysrKysrKwo+KzwKWyAtIDwgLSA+XSA8Wz4+Pj4+XSA+PiBbPj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+XQoKCg==

An example implementation, print T (true) if the two values are equal, F (false) if they are not equal
http://fatiherikli.github.io/brainfuck-visualizer/#KysrCj4KKysrKwo+KzwKWyAtIDwgLSA+XSA8Wz4+PgorKysrKysrKysrKysrKysrKysrKworKysrKysrKysrKysrKysrKysrKworKysrKysrKysrKysrKysrKysrKworKysrKysrKysrCi4KPgoKXSA+PiBbCisrKysrKysrKysrKysrKysrKysrCisrKysrKysrKysrKysrKysrKysrCisrKysrKysrKysrKysrKysrKysrCisrKysrKysrKysrKysrKysrKysrCisrKwouCj4KXQ==

Keatinge
  • 4,330
  • 6
  • 25
  • 44
  • very clever! im going to leave this open for a bit to see if any other ideas come around, but im going to use this for now. thanks! – Evorlor May 20 '16 at 20:43
  • 1
    @Evolor Because I need some number to be 1 in order to run code when the if statement evaluates to true, the code inside brackets only runs if the cell is not 0. I couldn't find a way to check if it evaluates to true, but I can check if it evaluates to false, and if its false move the pointer so that later the next if statement wont run because you will be at 0, otherwise you will be at that 1 and the code in the third brackets will run. (Sorry this explanation really sucks) – Keatinge May 20 '16 at 20:54
  • @Evorlor I feel like there might be some way to avoid using the third cell and do it based on the original 2 cells but I'm not smart enough to figure out how to do it that way. – Keatinge May 20 '16 at 20:55
  • Thanks :-) I'll let you know if I do, but I can't think of anything at the moment either – Evorlor May 20 '16 at 20:56
1
+>>(a+++++)>(b+++++)>>+<<<
[[->]<<]
<
[>>>>>-<<<<<
    a>b
]
>>
[->>-<
    a<b
]
>>
[-
    a=b
]

Pointer ends on the same pointer in the same state but the code within the appropriate brackets has been executed.

Josh
  • 11
  • 1
1

I came up with this for my bf compiler thing
basically it subtracts and then checks if the result is 0.
Can be easily changed to execute stuff in if/else-ish way

Layout: [A] B

>[-<->]+<[>-<[-]]>

Output

0 [result]

Result is 1 if equal

griffi-gh
  • 76
  • 5