reference:
http://www.geeksforgeeks.org/smallest-of-three-integers-without-comparison-operators/
Problem Definition:
Write a C program to find the smallest of three integers, without using any of the comparison operators.
Let 3 input numbers be x, y and z.
Solution:
Use method 2 of this post to find minimum of two numbers (We can’t use Method 1 as Method 1 uses comparison operator). Once we have functionality to find minimum of 2 numbers, we can use this to find minimum of 3 numbers.
Code:
/*Function to find minimum of x and y*/
int min(int x, int y)
{
return y + ((x - y) * ((x - y) >>
(sizeof(int) * CHAR_BIT - 1)));
}
/* Function to find minimum of 3 numbers x, y and z*/
int smallest(int x, int y, int z)
{
return min(x, min(y, z));
}