r/pythonhelp • • Oct 26 '22

what does that mean ?

what does x^=3 when x=5 mean ?

it evaluates to 6

3 Upvotes

3 comments sorted by

4

u/socal_nerdtastic Oct 26 '22

^ is the "bitwise xor" operator.

    0101 (decimal 5)
XOR 0011 (decimal 3)
  = 0110 (decimal 6)

https://en.wikipedia.org/wiki/Bitwise_operation#XOR

1

u/ab624 Oct 26 '22

cool, thanks

1

u/WikiSummarizerBot Oct 26 '22

Bitwise operation

XOR

A bitwise XOR is a binary operation that takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if only one of the bits is 1, but will be 0 if both are 0 or both are 1. In this we perform the comparison of two bits, being 1 if the two bits are different, and 0 if they are the same. For example: 0101 (decimal 5) XOR 0011 (decimal 3) = 0110 (decimal 6) The bitwise XOR may be used to invert selected bits in a register (also called toggle or flip).

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5