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.
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.
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:
(this is just to give you a hint, you will have to take care about equal numbers and similar issues.
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.
>>>>>>>>>++++++[>+++++++++++<-]>[>+>+>+<<<-]>+>-> <<<<<<<<<<<,>,< [->-<[>]<<]>>>[>>]>>>>>>>>.
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:
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.
Make the array like this. And point to the (4) i.e. point to the a
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.
No such thing exists in BF. The
>
and<
in BF move the pointer to the right and to the left, respectively.