How to check if two integers are equal without using comparison and arithmetic operators in Java

1 Answer

0 votes
public class MyClass {
    public static boolean equal(int x, int y) {
        return (x ^ y) == 0;
    }
    public static void main(String args[]) {
        int a = 12, b = 12;
 
        if (equal(a, b)) {
            System.out.printf("yes");
        }
        else {
            System.out.printf("no");
        }
    }
}



/*
run:

yes

*/

 



answered Nov 1, 2021 by avibootz
...