Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does 0 ^ 3 return 3 in java ? Shouldn't it be 0?

Im writing a simple program and one of my first step involves raising a number by 3.

public class ASum {
    public static void main(String[] args) {
        findNb(135440716410000L);
    }

    public static long findNb(long m) {
        int n = 0;
        System.out.println(0 ^ 3);

        return 0;
    }
}

On printout 0 ^ 3 returns 3. Like holy cow! 1 ^ 3 returns 2. Somebody help I’m going nuts.
Is ^ the same as the modulus operator?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It is a bitwise exclusive OR operator.

So you represent the numbers with 0s and 1s and an OR is made.

When one operator is 0 it is represented with zeros only so the result is the same as the other operator.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading