Created
July 16, 2018 15:39
-
-
Save GreggSetzer/86f60e8788f3a17a4c9913bc703deec1 to your computer and use it in GitHub Desktop.
JavaScript Interview Questions: Equality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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