3

For a CompSci class, we were reviewing the Java Math class. Being the foolish programmer that I am, I tried using the ^ operator instead of the Math.pow function. Surprise, Surprise, it did not work. But, what Java spit out, is my question. I am trying to figure out the operation that is being done with the numbers. You can see what I am 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, I get the following:

4
7
6
1
0
3
2
13
12

The same thing happens when I do the equivalent in Python (print 5^1, etc...). The Java API doc says that the ^ is a "bitwise exclusive OR", but that still does not help with how have gets 6 from 5 and 3. Could someone explain why this is happening?

3
  • Did you consider looking in the Java Language Specification? Commented Oct 26, 2012 at 2:22
  • 2
    Why the hell was this question closed? Are 10 answers not enough to prove that it is actually pretty easy to tell "what is being asked here"? Commented Oct 30, 2012 at 18:39
  • 1
    Why did you choose to ask here instead of simply googling bitwise exclusive OR? Commented Nov 1, 2012 at 13:39

8 Answers 8

15

This is a bitwise operation, and thus it operates on the binary bits of your numbers. 6 is 110 in binary form. 5 is 101 in binary form.

110
101
=== (^ xor)
011

011 is 3 in binary.

Read up on https://en.wikipedia.org/wiki/Exclusive_or

Sign up to request clarification or add additional context in comments.

Comments

3

Okay so lets take the first example. The easiest one. Take 5^1.

5 can be represented in binary like so 101 and one can be represented like so 001.

101    XOR
001
---
100 = 4 (in decimal)

Because 1 XOR 1 = 0, 0 XOR 0 = 0 and 1 XOR 0 = 1. Just go and check out the wiki page on XOR.

Comments

2

This is the XOR operation.

5 = 101 in binary
3 = 011 in binary

    101
XOR 011 
  = 110 = 6 in decimal

Comments

1

^ is BitWise XOR operator. So,

5 ^ 1: -

101 -> 5
001 -> 1
--- XOR
100 = 4
  • 1 XOR 1 = 0
  • 1 XOR 0 = 1

See these links: -

Comments

1

^ is the bitwise XOR operation, and it's truth table looks like this:

  | 0 | 1 |
0 | 0 | 1 |
1 | 1 | 0 |

So the output bit is 1 only when the inputs are different, and 0 otherwise. So 5 ^ 3 looks like this:

  0000 0101 (5)
^ 0000 0011 (3)
= 0000 0110 (6)

So it's doing exactly what it's supposed to. Take a look at the Nuts and Bolts tutorial for bitwise operations and this SO question for more references.

1 Comment

Best answer by far, because of the truth table. +1
1

It's because of binary representation of five is 101b, of three is 011b.

Xor works in the following way:

0 xor 0 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0

Therefore: 101 xor 011 = 110 that is binary representation of six.

Comments

1

In Java, the ^ operator when applied to int arguments is bitwise exclusive-or. That explains the output. For the particular case of 5 and 3:

101 (5)
011 (3)
---
110 (6)

Note that for logical arguments, it is logical exclusive-or. In that case (b1 ^ b2) == (b1 != b2).

Comments

1

Hope this little program helps to explain the exclusive-or operator better using the examples it prints:

>>> for x, y in ((x, y) for x in range(11) for y in range(x, 11)):
    print('''{0} ^ {1} = {2} (Why?)
Because:
{0:04b} = {0}
^^^^
{1:04b} = {1}
====
{2:04b} = {2}
'''.format(x, y, x ^ y))


0 ^ 0 = 0 (Why?)
Because:
0000 = 0
^^^^
0000 = 0
====
0000 = 0

0 ^ 1 = 1 (Why?)
Because:
0000 = 0
^^^^
0001 = 1
====
0001 = 1

0 ^ 2 = 2 (Why?)
Because:
0000 = 0
^^^^
0010 = 2
====
0010 = 2

0 ^ 3 = 3 (Why?)
Because:
0000 = 0
^^^^
0011 = 3
====
0011 = 3

0 ^ 4 = 4 (Why?)
Because:
0000 = 0
^^^^
0100 = 4
====
0100 = 4

0 ^ 5 = 5 (Why?)
Because:
0000 = 0
^^^^
0101 = 5
====
0101 = 5

0 ^ 6 = 6 (Why?)
Because:
0000 = 0
^^^^
0110 = 6
====
0110 = 6

0 ^ 7 = 7 (Why?)
Because:
0000 = 0
^^^^
0111 = 7
====
0111 = 7

0 ^ 8 = 8 (Why?)
Because:
0000 = 0
^^^^
1000 = 8
====
1000 = 8

0 ^ 9 = 9 (Why?)
Because:
0000 = 0
^^^^
1001 = 9
====
1001 = 9

0 ^ 10 = 10 (Why?)
Because:
0000 = 0
^^^^
1010 = 10
====
1010 = 10

1 ^ 1 = 0 (Why?)
Because:
0001 = 1
^^^^
0001 = 1
====
0000 = 0

1 ^ 2 = 3 (Why?)
Because:
0001 = 1
^^^^
0010 = 2
====
0011 = 3

1 ^ 3 = 2 (Why?)
Because:
0001 = 1
^^^^
0011 = 3
====
0010 = 2

1 ^ 4 = 5 (Why?)
Because:
0001 = 1
^^^^
0100 = 4
====
0101 = 5

1 ^ 5 = 4 (Why?)
Because:
0001 = 1
^^^^
0101 = 5
====
0100 = 4

1 ^ 6 = 7 (Why?)
Because:
0001 = 1
^^^^
0110 = 6
====
0111 = 7

1 ^ 7 = 6 (Why?)
Because:
0001 = 1
^^^^
0111 = 7
====
0110 = 6

1 ^ 8 = 9 (Why?)
Because:
0001 = 1
^^^^
1000 = 8
====
1001 = 9

1 ^ 9 = 8 (Why?)
Because:
0001 = 1
^^^^
1001 = 9
====
1000 = 8

1 ^ 10 = 11 (Why?)
Because:
0001 = 1
^^^^
1010 = 10
====
1011 = 11

2 ^ 2 = 0 (Why?)
Because:
0010 = 2
^^^^
0010 = 2
====
0000 = 0

2 ^ 3 = 1 (Why?)
Because:
0010 = 2
^^^^
0011 = 3
====
0001 = 1

2 ^ 4 = 6 (Why?)
Because:
0010 = 2
^^^^
0100 = 4
====
0110 = 6

2 ^ 5 = 7 (Why?)
Because:
0010 = 2
^^^^
0101 = 5
====
0111 = 7

2 ^ 6 = 4 (Why?)
Because:
0010 = 2
^^^^
0110 = 6
====
0100 = 4

2 ^ 7 = 5 (Why?)
Because:
0010 = 2
^^^^
0111 = 7
====
0101 = 5

2 ^ 8 = 10 (Why?)
Because:
0010 = 2
^^^^
1000 = 8
====
1010 = 10

2 ^ 9 = 11 (Why?)
Because:
0010 = 2
^^^^
1001 = 9
====
1011 = 11

2 ^ 10 = 8 (Why?)
Because:
0010 = 2
^^^^
1010 = 10
====
1000 = 8

3 ^ 3 = 0 (Why?)
Because:
0011 = 3
^^^^
0011 = 3
====
0000 = 0

3 ^ 4 = 7 (Why?)
Because:
0011 = 3
^^^^
0100 = 4
====
0111 = 7

3 ^ 5 = 6 (Why?)
Because:
0011 = 3
^^^^
0101 = 5
====
0110 = 6

3 ^ 6 = 5 (Why?)
Because:
0011 = 3
^^^^
0110 = 6
====
0101 = 5

3 ^ 7 = 4 (Why?)
Because:
0011 = 3
^^^^
0111 = 7
====
0100 = 4

3 ^ 8 = 11 (Why?)
Because:
0011 = 3
^^^^
1000 = 8
====
1011 = 11

3 ^ 9 = 10 (Why?)
Because:
0011 = 3
^^^^
1001 = 9
====
1010 = 10

3 ^ 10 = 9 (Why?)
Because:
0011 = 3
^^^^
1010 = 10
====
1001 = 9

4 ^ 4 = 0 (Why?)
Because:
0100 = 4
^^^^
0100 = 4
====
0000 = 0

4 ^ 5 = 1 (Why?)
Because:
0100 = 4
^^^^
0101 = 5
====
0001 = 1

4 ^ 6 = 2 (Why?)
Because:
0100 = 4
^^^^
0110 = 6
====
0010 = 2

4 ^ 7 = 3 (Why?)
Because:
0100 = 4
^^^^
0111 = 7
====
0011 = 3

4 ^ 8 = 12 (Why?)
Because:
0100 = 4
^^^^
1000 = 8
====
1100 = 12

4 ^ 9 = 13 (Why?)
Because:
0100 = 4
^^^^
1001 = 9
====
1101 = 13

4 ^ 10 = 14 (Why?)
Because:
0100 = 4
^^^^
1010 = 10
====
1110 = 14

5 ^ 5 = 0 (Why?)
Because:
0101 = 5
^^^^
0101 = 5
====
0000 = 0

5 ^ 6 = 3 (Why?)
Because:
0101 = 5
^^^^
0110 = 6
====
0011 = 3

5 ^ 7 = 2 (Why?)
Because:
0101 = 5
^^^^
0111 = 7
====
0010 = 2

5 ^ 8 = 13 (Why?)
Because:
0101 = 5
^^^^
1000 = 8
====
1101 = 13

5 ^ 9 = 12 (Why?)
Because:
0101 = 5
^^^^
1001 = 9
====
1100 = 12

5 ^ 10 = 15 (Why?)
Because:
0101 = 5
^^^^
1010 = 10
====
1111 = 15

6 ^ 6 = 0 (Why?)
Because:
0110 = 6
^^^^
0110 = 6
====
0000 = 0

6 ^ 7 = 1 (Why?)
Because:
0110 = 6
^^^^
0111 = 7
====
0001 = 1

6 ^ 8 = 14 (Why?)
Because:
0110 = 6
^^^^
1000 = 8
====
1110 = 14

6 ^ 9 = 15 (Why?)
Because:
0110 = 6
^^^^
1001 = 9
====
1111 = 15

6 ^ 10 = 12 (Why?)
Because:
0110 = 6
^^^^
1010 = 10
====
1100 = 12

7 ^ 7 = 0 (Why?)
Because:
0111 = 7
^^^^
0111 = 7
====
0000 = 0

7 ^ 8 = 15 (Why?)
Because:
0111 = 7
^^^^
1000 = 8
====
1111 = 15

7 ^ 9 = 14 (Why?)
Because:
0111 = 7
^^^^
1001 = 9
====
1110 = 14

7 ^ 10 = 13 (Why?)
Because:
0111 = 7
^^^^
1010 = 10
====
1101 = 13

8 ^ 8 = 0 (Why?)
Because:
1000 = 8
^^^^
1000 = 8
====
0000 = 0

8 ^ 9 = 1 (Why?)
Because:
1000 = 8
^^^^
1001 = 9
====
0001 = 1

8 ^ 10 = 2 (Why?)
Because:
1000 = 8
^^^^
1010 = 10
====
0010 = 2

9 ^ 9 = 0 (Why?)
Because:
1001 = 9
^^^^
1001 = 9
====
0000 = 0

9 ^ 10 = 3 (Why?)
Because:
1001 = 9
^^^^
1010 = 10
====
0011 = 3

10 ^ 10 = 0 (Why?)
Because:
1010 = 10
^^^^
1010 = 10
====
0000 = 0

>>> 

1 Comment

People are fairly good at recognizing patterns (ie, language). A rule should be rather easily derived from the examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.