Comparing two integers without any comparison

前端 未结 14 1748
温柔的废话
温柔的废话 2020-12-05 16:06

Is it possible to find the greatest of two integers without any comparison? I found some solutions:

if(!(a/b)) // if a is less than b then division result          


        
14条回答
  •  自闭症患者
    2020-12-05 16:40

    void greater(int a, int b) {
        int c = a - b;
        switch(c) {
            case 0:
                cout << "a and b are equal" << endl;
                break;
            default:
                int d = c & (1<<31);
                switch(d) {
                    case 0:
                        cout << "a is bigger than b" << endl;
                        break;
                    default:
                        cout << "a is less than b" << endl;
                }
        }
    }
    

提交回复
热议问题