Write a C program to find the smallest of three integers, without using any of the comparison operators.
Method 2 (Use Bit Operations)
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.
Method 3 (Use Division operator)
Method 1 (Repeated Subtraction)
Read full article from Smallest of three integers without comparison operators | GeeksforGeeks
Method 2 (Use Bit Operations)
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.
int
min(
int
x,
int
y)
{
return
y + ((x - y) & ((x - y) >>
(
sizeof
(
int
) * CHAR_BIT - 1)));
}
int
smallest(
int
x,
int
y,
int
z)
{
return
min(x, min(y, z));
}
int
smallest(
int
x,
int
y,
int
z)
{
if
(!(y/x))
// Same as "if (y < x)"
return
(!(y/z))? y : z;
return
(!(x/z))? x : z;
}
int
smallest(
int
x,
int
y,
int
z)
{
int
c = 0;
while
( x && y && z )
{
x--; y--; z--; c++;
}
return
c;
}