+ 1

# print(10^2) will display 8 in python. so what ^ means?

I think this doesn't need any detail

8 odpowiedzi

+ 6

This operator, ^, is the Exclusive OR operator (often abbreviated as XOR). It is one of the logical operators that perform bit-by-bit logic and return a value instead of simply True/False. Think of XOR as an operator that flips bits to the opposite value wherever the bitmask has a 1 bit.
In 10^2, 2 is the bitmask.
It looks like this in binary,
00001010: 10
XOR
00000010: 2
becomes
00001000: 8
The 2 bit in the value 10 gets toggled to its opposite value from 1 to 0. So 8 is what is leftover.

+ 3

bitwise XOR

+ 3

It only seems "random" because you are thinking in decimal numbers.
But computers inherently work in binary, so some calculations can be expressed much more efficiently with binary operators.
There are very specific use cases for binary XOR, for example in cryptography or in graphics programming.
https://stackoverflow.com/questions/2096916/real-world-use-cases-of-bitwise-operators
If you look at the binary form, you could consider each 1 and 0 as a "flag", a true or false value. So comparing two numbers with XOR, will give you the flags which are true for only one of the numbers. One example could be mutually exclusive features or activities, for example in a shop you cannot make a card payment and a cash payment at the same time.

+ 2

Brian Oh, no! Now you have given any detail!

+ 2

Thanks... I think I understand... kinda lol, (I do understand)

+ 1

Lisa Haha. I think the OP meant that the detail section of the post didn't need further elaboration from the info in the title.

+ 1

When would this be used in code?

+ 1

It seems so... random is the best word I can come up with right now