+ 1

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

I think this doesn't need any detail

19th Jun 2024, 12:43 PM
Syarina Ramadhani
Syarina Ramadhani - avatar
8 Respostas
+ 7
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.
19th Jun 2024, 4:04 PM
Brian
Brian - avatar
+ 3
bitwise XOR
19th Jun 2024, 12:52 PM
Lisa
Lisa - avatar
+ 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.
20th Jun 2024, 8:09 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Brian Oh, no! Now you have given any detail!
19th Jun 2024, 4:09 PM
Lisa
Lisa - avatar
+ 2
Thanks... I think I understand... kinda lol, (I do understand)
21st Jun 2024, 10:53 PM
Dominic Hetrick
Dominic Hetrick - avatar
+ 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.
19th Jun 2024, 4:13 PM
Brian
Brian - avatar
+ 1
When would this be used in code?
20th Jun 2024, 6:17 PM
Dominic Hetrick
Dominic Hetrick - avatar
+ 1
It seems so... random is the best word I can come up with right now
20th Jun 2024, 6:18 PM
Dominic Hetrick
Dominic Hetrick - avatar