找出較大值

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)];
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章