Find if x is greater than y using the bitwise operator in C

If x> y, then this function will return 1, another wise return is 0.

I still have

int isitGreater(int x, int y) {

     return (((y+((~x)+1)) >> 31) & 1);

but it does not work.

Allowed Operating Systems: Legal ops :! ~ and ^ | + <→

I'm sure I have logic, if X is Y and I get a negative number, it means y> x, so the 32nd bit is 1, so I shift this bit to the right 31 times and then "and" with "1" .

edit: this does not work if x is negative, due to overflow. how can i fix this overflow problem without using conditional statements?

0
source share
2 answers

Your code works fine for me. Submit a valid question.

: , x -2147483648, - (- 2147483648) (, , ~ (-2147483648) +1) .

+3

2 : -2147483648 [0x80000000].

+1

Source: https://habr.com/ru/post/1696384/


All Articles