Click here to Skip to main content
15,914,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am working on computing Xor operation, but i could not exactly compute the proper result. Please help me to find a solution.

If we "true" and "false", how to compute XOR operation to get the final result?

Regards,
Amal Raj

What I have tried:

I have tried like:
result = s1 ^ s2; // here s1= "true", s2= "false"
Posted
Updated 20-Jun-16 0:58am
Comments
Herman<T>.Instance 20-Jun-16 1:59am    
see HERE

The problem is, you are using the Boolean values as string ["true","false"] instead of [true, false]
if you use string values you will the result as 0 in all the combination

try parsing it to a boolean value and XOR it
JavaScript
var s1 = "true", s2 = "false";
        var s1Bool = s1 === "true"; // true
        var s2Bool = s2 === "true"; // false  
        result = s1Bool ^ s2Bool; // 1
 
Share this answer
 
^ is the JS XOR, and it works:
HTML
<!DOCTYPE html>
<html>
<body>

<p>This example calls a function which performs a calculation, and returns the result:</p>

<p id="demo"></p>

<script>
function doXOR(a,b) {
var result = a ^ b;
return result;
}
document.getElementById("demo").innerHTML = doXOR(true, true);
document.getElementById("demo").innerHTML += doXOR(true, false);
document.getElementById("demo").innerHTML += doXOR(false, false);
document.getElementById("demo").innerHTML += doXOR(false, true);
</script>

</body>
</html>

It gives 0101 when executed for me...I'd check your code is executing.
 
Share this answer
 
You have been a member here long enough to know better than to post the same question[^] in multiple forums.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900