本文共 627 字,大约阅读时间需要 2 分钟。
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.
Use method 2 of (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.
/*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));}
转载地址:http://gexti.baihongyu.com/