找出较大值

来源:互联网 发布:网络机柜42u尺寸 编辑:程序博客网 时间:2024/04/30 08:49

There are two int variables: a and b, don’t use “if”, “? :”, “switch” or other judgement statements, find out the biggest one of the two numbers.

// 如果 i >= 0,返回0。否则返回1。在 max() 中被调用。
inline int signof(int i)
{
    
return unsigned(i) >> (sizeof (int) * 8 - 1);
}


// 返回两个整型参数中数值较大的参数的值。
int max(int a, int b)
{
    
int p[2];
    p[0] = a;
    p[1] = b;

    
return p[signof(a - b)];
}

原创粉丝点击