+ 4

[SOLVED] Help with explanation of XOR and ^ in Java

Hi all! Pls tell me what mean XOR ^ and where we can real use it. But pls tell the answer a maximally simple - I'm a newcomer :)

25th Feb 2018, 7:25 PM
Grey King
Grey  King - avatar
6 Antworten
+ 14
there are many usages, in various fields, from neural networks to electric circuitry or cryptography... here's a fun one: 1 light bulb (BULB), 2 switches (A, B) which act as a XOR gate ______________ / ---|-----(A) (B)-----------(BULB)--| | _____________ / | |_____________________________________| assume this: A pointing up as 1 A pointing down as 0 B pointing up as 0 B pointing down as 1 the truth table would look like this: A | B | BULB ----------------------------------- 0 0 0 0 1 1 1 0 1 1 1 0 so you can see that for a closed circuit we need either A=1, B=0 or A=0, B=1 which will result in a closed circuit that activates the light bulb if both A, B are 1 or both are 0, the circuit is open with no juice for our little light bulb :( so if you ever encountered a set of two switches that alternate the ON/OFF state of one light bulb, this is how it basically works, with just a plain little magical XOR gate ^_^ example been modified from: http://echochamber.me/viewtopic.php?t=28412
25th Feb 2018, 10:27 PM
Burey
Burey - avatar
+ 16
^ is xor operator. It gives 1 if the operands are different, and gives 0 if they are same. 0^0 = 0 0^1 = 1 1^0 = 1 1^1 = 0 Now, if you perform xor between two numbers like this: 5^9, it'll convert the numbers into equivalent binary numbers and implement xor bit by bit. That's why it is called bitwise xor operator. 5 ^ 9 = binary 5 ^ binary 9 = 0101 ^ 1001 = 0^1..1^0..0^0..1^1 = 1100 = 12 More on bitwise xor: https://www.sololearn.com/learn/4074/?ref=app
25th Feb 2018, 8:03 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 12
already explained wonderfully by Shamima Yasmin and ChaoticDawg so i'll just leave this here this might help you to understand the logic behind it better https://code.sololearn.com/WBOdrCZZT0a7/?ref=app
25th Feb 2018, 9:31 PM
Burey
Burey - avatar
+ 9
First you need to understand binary and the binary bitwise operators & (and) | (or). Binary or base 2 uses only the digits 0 and 1 to represent a number or character etc. In this case we will use integer values as they are the simplest to understand. The ones place is the furthest place to the right and each place to the left of it is twice the value of the place immediately to its right. Each place is summed up to result in the integer that is being represented. 0 = 0000 1 = 0001 2 = 0010 3 = 0011 4 = 0100 ... The & (and) operator requires both bits to be 1 in order for the bit in that place to result in 1, otherwise the resulting bit for that place is 0. Lets take the integer numbers 5 and 8 as an example. 5 = 0101 8 = 1000 & = 0000 = 0 If we used 6 instead of 8: 5 = 0101 6 = 0110 & = 0100 = 4 The | (or) operator results in 1 if either bit or both bits in that place are 1, otherwise the resulting bit for that place is 0. Lets take the integer numbers 5 and 8 as an example. 5 = 0101 8 = 1000 | = 1101 = 13 If we used 6 instead of 8: 5 = 0101 6 = 0110 | = 0111 = 7 The ^ (xor) operator results in 1 only if one bit in that place is 1, otherwise the resulting bit for that place is 0. Lets take the integer numbers 5 and 8 as an example. 5 = 0101 8 = 1000 ^ = 1101 = 13 If we used 6 instead of 8: 5 = 0101 6 = 0110 ^ = 0011 = 3
25th Feb 2018, 8:19 PM
ChaoticDawg
ChaoticDawg - avatar
0
Burey I see! It's a best explanation, sorry other guys! But tell me what that for? I mean why we will use 5^7 and recived 8 (example)?
25th Feb 2018, 9:38 PM
Grey King
Grey  King - avatar
- 4
Google "what is xor in java" it should clear it for you
25th Feb 2018, 7:47 PM
Farshaad Heydari
Farshaad Heydari - avatar