+ 2

What does the ^ operand do?

I discovered the ^ by accident while doing print(5^2).. it gives bizarre answers and I couldn't make out what it was for, any help?

9th Jan 2017, 3:09 AM
ramzi
ramzi - avatar
7 ответов
+ 5
What Toby didn't explain is that this is a bitwise operator, that compares bits, instead of conditions. The first test you mentioned, 2 ^ 3, is like this: 2 = 010 (only showing 3 bits, for simplicity) 3 = 011 If you look at them that way, you can see that the only bit that applies to the condition is the last one, therefore, 010 ^ 011 = 001. Same thing for the other example: 2 = 010 4 = 100 The result of 010 ^ 100 is 110, which is 6.
9th Jan 2017, 3:51 AM
Dao
Dao - avatar
+ 4
XOR XOR you need to satisfy EXACTLY one of the criteria and can't have both Joe is male and 25, Bob is male and 30, Anne is female and 25. If we search for people who are "male AND 25" we will only get Joe. If we search for "male OR 25" then we will get all three people. If we search "male XOR 25" we will get Bob and Anne, but we won't get Joe because he satisfies both of the criteria.
9th Jan 2017, 3:24 AM
Toby Milroy
Toby Milroy - avatar
+ 1
oh okay makes perfect sense thanks a ton
9th Jan 2017, 3:26 AM
ramzi
ramzi - avatar
+ 1
Eum however I just went back to try print(2^3) and got 1 as an answer, whereas print(2^4) gives 6 as an answer... am I still missing something?
9th Jan 2017, 3:29 AM
ramzi
ramzi - avatar
+ 1
Oh wow I never would've had understood that without your comment + add to that i've been answered by ken kaneki, making this even more epic! XD .. thanks for the help :)
9th Jan 2017, 4:22 AM
ramzi
ramzi - avatar
+ 1
that is for power ..bitwise operator
9th Jan 2017, 8:47 AM
SURAJ NAGARE
SURAJ NAGARE - avatar
+ 1
^ is both the XOR operator and the symmetric difference operator in sets
9th Jan 2017, 8:12 PM
Amaras A
Amaras A - avatar