Bitwise Less than or Equal to

前端 未结 5 1904
-上瘾入骨i
-上瘾入骨i 2021-01-19 02:49

There seems to be some kind of misconception that this is for a contest. I\'m trying to work through an assignment and I\'ve been stuck on this for an hour now.

<         


        
5条回答
  •  终归单人心
    2021-01-19 03:25

    Those functions don't fully work because of the overflow, so that's how I solved the problem. Eh...

    int isLessOrEqual(int x, int y) {
    int diff_sgn = !(x>>31)^!(y>>31);      //is 1 when signs are different
    int a = diff_sgn & (x>>31);            //diff signs and x is neg, gives 1
    int b = !diff_sgn & !((y+(~x+1))>>31); //same signs and difference is pos or = 0, gives 1
    int f = a | b;
    return f;
    }
    

提交回复
热议问题