Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing "==" with bitwise operators

Using only bitwise operators (|, &, ~, ^, >>, <<) and other basic operators like +, -, and !, is it possible to replace the "==" below?

int equal(int x, int y) {
    return x == y;
}
like image 774
not_l33t Avatar asked Sep 06 '25 03:09

not_l33t


1 Answers

Remember that an XOR is the exactly same as NOT EQUALS and XNOR is exactly the same as EQUALS. So, the following will give you exactly what you want:

return !(x ^ y);
like image 149
Ignacio Vazquez-Abrams Avatar answered Sep 08 '25 00:09

Ignacio Vazquez-Abrams