Skip to content

Instantly share code, notes, and snippets.

@GreggSetzer
Created July 16, 2018 15:39
Show Gist options
  • Save GreggSetzer/86f60e8788f3a17a4c9913bc703deec1 to your computer and use it in GitHub Desktop.
Save GreggSetzer/86f60e8788f3a17a4c9913bc703deec1 to your computer and use it in GitHub Desktop.
JavaScript Interview Questions: Equality
/*
Write a function that determines whether two integers are equal without using any comparison operators.
Use the bitwise XOR operator.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_XOR
Using XOR, if any of the bits are different, a 1 is returned, otherwise, a 0 is returned.
*/
function isEqual(n1, n2) {
return (n1 ^ n2) === 0;
}
console.log(isEqual(2,2)); //True
console.log(isEqual(1,2)); //False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment