-2

In python if you want to sqaure an int you have to put ** but in java you put ^; But python didn't give me an error when I did ^ in my code

It just gave me something with no pattern can anyone explain what happens when you use ^ in python? enter image description here

Reptide
  • 1
  • 1

1 Answers1

1

In python, ** is the exponentiation operator, and ^ is the XOR operator (python operators)

XOR is the bitwise XOR of the two numbers. In other words, write each number in binary, then take XOR of the two numbers one place at a time (A XOR B is true if A and B are different, and false if they're the same. 0 is false, 1 is true).

Here's an example computing 5 XOR 3:

5 ^ 3
101 = 5
011 = 3

from left to right:

1 ^ 0 -> 1
0 ^ 1 -> 1
1 ^ 1 -> 0
0 ^ 0 -> 0
  101
^ 011
-------
  110

so 5^3=6

Tarik
  • 10,810
  • 2
  • 26
  • 40
Danny Rivers
  • 56
  • 1
  • 4