index.js
function binadd(bin1, bin2){
  var i1 = bin1.length;
  var i2 = bin2.length;
  var i3 = (i1 > i2) ? (i1 + 1) : (i2 + 1);
  var result = new Array(i3);
  var c = 0;
  // Add the two arrays as long as there exist elements in the arrays
  while((i1 > 0) && (i2 > 0)){
    i1--;
    i2--;
    i3--;
    if(bin1[i1]){c++;}
    if(bin2[i2]){c++;}
    result[i3] = (c%2) ? true : false;
    c >>= 1;
  }
  // copy the remaining elements
  if(i1){
    for(var i=0; i<i1; i++){result[i+1] = bin1[i];}
  }else{
    for(var i=0; i<i2; i++){result[i+1] = bin2[i];}
  }
  // add the remaining carry
  var carry = c ? true : false;
  while(c && (i3 > 1)){
    i3--;
    result[i3] = !result[i3];
    c = !result[i3];
  }
  // Return result with carry if necessary
  if (c==1){
    result[0] = true;
    return result;
  }else{
    return result.slice(1);
  }
}
function binadd(bin1, bin2){
  var i1 = bin1.length;
  var i2 = bin2.length;
  var i3 = (i1 > i2) ? (i1 + 1) : (i2 + 1);
  var result = new Array(i3);
  var c = 0;
  // Add the two arrays as long as there exist elements in the arrays
  while((i1 > 0) && (i2 > 0)){
    i1--;
    i2--;
    i3--;
    if(bin1[i1]){c++;}
    if(bin2[i2]){c++;}
    result[i3] = (c%2) ? true : false;
    c >>= 1;
  }
  // copy the remaining elements
  if(i1){
    for(var i=0; i<i1; i++){result[i+1] = bin1[i];}
  }else{
    for(var i=0; i<i2; i++){result[i+1] = bin2[i];}
  }
  // add the remaining carry
  var carry = c ? true : false;
  while(c && (i3 > 1)){
    i3--;
    result[i3] = !result[i3];
    c = !result[i3];
  }
  // Return result with carry if necessary
  if (c==1){
    result[0] = true;
    return result;
  }else{
    return result.slice(1);
  }
}
function binadd ( bin1 , bin2 ) { var i1 = bin1 . length ; var i2 = bin2 . length ; var i3 = ( i1 > i2 ) ? ( i1 + 1 ) : ( i2 + 1 ) ; var result = new Array ( i3 ) ; var c = 0 ; // Add the two arrays as long as there exist elements in the arrays while ( ( i1 > 0 ) && ( i2 > 0 ) ) { i1 -- ; i2 -- ; i3 -- ; if ( bin1 [ i1 ] ) { c ++ ; } if ( bin2 [ i2 ] ) { c ++ ; } result [ i3 ] = ( c % 2 ) ? true : false ; c >>= 1 ; } // copy the remaining elements if ( i1 ) { for ( var i = 0 ; i < i1 ; i ++ ) { result [ i + 1 ] = bin1 [ i ] ; } } else { for ( var i = 0 ; i < i2 ; i ++ ) { result [ i + 1 ] = bin2 [ i ] ; } } // add the remaining carry var carry = c ? true : false ; while ( c && ( i3 > 1 ) ) { i3 -- ; result [ i3 ] = ! result [ i3 ] ; c = ! result [ i3 ] ; } // Return result with carry if necessary if ( c == 1 ) { result [ 0 ] = true ; return result ; } else { return result . slice ( 1 ) ; } }
Addition of two binary arrays
Addition of two binary arrays