Created
July 20, 2017 20:16
-
-
Save wjramos/b78e722d24ed04c7ad6c0265ddd28648 to your computer and use it in GitHub Desktop.
bitCounter
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 addBinary( strA, strB ) { | |
let result = ''; | |
let carry = false; | |
for ( let i = Math.max( strA.length, strB.length ); i > -1; i-- ) { | |
result += addBits( strA[ i ] ? parseInt( strA[ i ] ) : 0, strB[ i ] ? parseInt( strB[ i ] ) : 0 ); | |
} | |
return result; | |
function addBits( bitA = 0, bitB = 0 ) { | |
if ( ( carry && ( ( bitA && bitB ) || ( !bitA && !bitB ) ) ) || ( ( bitA || bitB ) && ( !bitA || !bitB ) ) ) { | |
return '1'; | |
} | |
carry = ( bitA && bitB ) || ( carry && ( bitA || bitB ) ); | |
return '0'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment