Created
July 15, 2015 09:38
-
-
Save Aleksey-Danchin/701742c6872c59d3e0a4 to your computer and use it in GitHub Desktop.
Bitwise suma.
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
function sum (a, b) { | |
var c = [], buff = false; | |
for (var i = a.length - 1; i >= 0; i--) | |
if ((a[i] && b[i]) || (!a[i] && !b[i])) { | |
c.unshift(buff); | |
buff = a[i]; | |
} else c.unshift(!buff); | |
if (buff) c.unshift(true); | |
return c; | |
} |
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
// firstBitArray = 101101011011 | |
// secondBitArray = 101111010001 | |
// result = 1011100101100 | |
var firstBitArray = [true, false, true, true, false, true, false, true, true, false, true, true]; | |
var secondBitArray = [true, false, true, true, true, true, false, true, false, false, false, true]; | |
var result = sum(firstBitArray, secondBitArray); | |
// result === [ true, false, true, true, true, false, false, true, false, true, true, false, false ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment