Last active
January 4, 2017 13:24
-
-
Save mostafahussein/01e75d46777c35cf07ad86ad3d2513d6 to your computer and use it in GitHub Desktop.
Add two numbers using bitwise operators https://jsfiddle.net/deadshot/ugycbacd/
This file contains hidden or 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
// Based on the following answer : http://codegolf.stackexchange.com/questions/20996/add-without-addition-or-any-of-the-4-basic-arithmetic-operators/21025#21025 | |
var x = 6; | |
var y = 6; | |
var result = function () { | |
var sum = 0 | |
while (y) { | |
sum = x^=y,y=(y&x^y)<<1 | |
} | |
return sum | |
}; | |
alert(result()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment