Skip to content

Instantly share code, notes, and snippets.

@mostafahussein
Last active January 4, 2017 13:24
Show Gist options
  • Save mostafahussein/01e75d46777c35cf07ad86ad3d2513d6 to your computer and use it in GitHub Desktop.
Save mostafahussein/01e75d46777c35cf07ad86ad3d2513d6 to your computer and use it in GitHub Desktop.
Add two numbers using bitwise operators https://jsfiddle.net/deadshot/ugycbacd/
// 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