Hi Gurus,
Its somewhat frustrating question.
Is there any way to compare two numbers without using any conditional operator like (<,>) ?
thanx,
-piyu
Printable View
Hi Gurus,
Its somewhat frustrating question.
Is there any way to compare two numbers without using any conditional operator like (<,>) ?
thanx,
-piyu
why cant you use the '<','>', etc. operators ?
What do you mean under "to compare two numbers" ?
See,
Suppose a = 10 and b = 20. are two numbers.
i want to know which is greater out of them. For this purpose we can use '<','>' operators. but i don't want to use them to achieve the result as 20.
Is there any way out ?
You dont want to use:
?PHP Code:
int a = 10 ;
int b = 20 ;
if(a>b)
{
// do something
}
if(b>a)
{
// do something
}
if(20==b)
{
// do something
}
Yah, inequality operators are terrible for determining order.
I mean what does one have to do with the other.
yes, TheRogue
I really don't want to use these operators.
I am also thinking that we can' t do this.. but still i wan't to raise on CODEGURU coz there are really good ppls who can answer it well.
-Piyu
Well, I am just curious. Please tell us why you don't want to use
them.
Pretty please..with sugar on top.:)
Hi souldog,
Thanx for being curios with the question.
See.
I want to write an assembly language code for one specific microprocessor which doesn't contain any cmp ('compare' ), jl('jump if less'), jg (jump if greater) instructions. so i am thinking how it is possible to find greatest number out of A and B.
May be my approach is wrong. isn't it ?
please suggest.
-Piyu
If it has a check for zero,
could you decrement both variables until one of them is zero ?
Well if you can look at the bits in the numbers starting with
the MSB and find the first place they are different, then the number
that has a 1 in this bit is larger.
hi TheRogue
---------------
If it has a check for zero,
could you decrement both variables until one of them is zero ?
----------------
Fortunatly, i have that instruction with me.
I can able to decrement both variable until one of them is ZERO.
now please clarify..
regards,
-Piyu
start:
decrement both variables
if neither variable has reached zero, goto start:
if one variable has reached zero it is smaller
if both variables have reached zero they are the same
ps. what is the microprocessor ?
Do you have a carry flag in a FLAGS register. You can subtract the two numbers and see if a borrow was needed.