Is there any way of finding the greater of two numbers without using any logical operators?
Yeah there is. It's pretty straight forward:
max(a,b) = 1/2(a+b + abs(a-b)); abs(x) being the absolute value of x.
That's because,
If a > b, 1/2(a+b + a-b) = a.
If b > a, 1/2(a+b + b-a) = b.
Another one I can think of is the following - though it involves the use of an if block:
if(abs(a-b)-(a-b))
{
//Code of a < b follows
}
else
{
//Code of a >= b follows
}
Please suggest any others you can think of.

1 comment:
Hello Vijay,
This Rahul here from Devils Workshop!
I have been long term reader of your blog MATRIX CHRONICLE!
I was looking for it after long time and found that it does not exist anymore in ur blog listing!
What happened to ur account??
Did u moved it??
Actually I have around 10 posts on my blog linking to ur site! If you have moved ur blog, let me know so that I can update my links or otherwise can delete my posts!
Its too embarrassing when someone report broken link and u can not fix it! :-(
Looking for prompt reply,
Rahul Bansal
Email: rahul286 [at] gmail [dot] com
Post a Comment