Mathematically Find Max Value without Conditional Comparison

前端 未结 14 605
暗喜
暗喜 2020-12-03 03:39

----------Updated ------------

codymanix and moonshadow have been a big help thus far. I was able to solve my problem using the equations and instead of using right

14条回答
  •  萌比男神i
    2020-12-03 04:00

    You can express this as a series of arithmetic and bitwise operations, e.g.:

    int myabs(const int& in) {
      const int tmp = in >> ((sizeof(int) * CHAR_BIT) - 1);
      return tmp - (in ^ tmp(;
    }
    
    int mymax(int a, int b) {
        return ((a+b) + myabs(b-a)) / 2;
    }
    

提交回复
热议问题