Java and python ^ operators

For the CompSci class, we are looking at the Java math class As my stupid programmer, I try to use the ^ operator instead of math POW function Surprise, surprise, it's useless But what Java spits out is my problem I'm trying to figure out what I'm doing with numbers You can see what I'm talking about below

System.out.println(5^1);
System.out.println(5^2);
System.out.println(5^3);
System.out.println(5^4);
System.out.println(5^5);
System.out.println(5^6);
System.out.println(5^7);
System.out.println(5^8);
System.out.println(5^9);

Running the above content, I get the following:

4
7
6
1
0
3
2
13
12

The same thing happens when I use the equivalent in python (print 5 ^ 1, etc.) The Java API documentation says ^ is a bitwise XOR, but it still doesn't help how to get 6 from 5 and 3 Can anyone explain why this happened?

Solution

This is a bitwise operation, so it operates on the binary bits of a number 110 in Fig. 6 is in binary form 5 is 101 in binary form

110
101
=== (^ xor)
011

011 is binary 3

read https://en.wikipedia.org/wiki/Exclusive_or

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>