Bitwise AND of N binary strings | Helper method : given two unequal sized bit strings , converts them to same length by adding leading 0 s in the smaller string . Returns the the new length ; Return len_b which is highest . No need to proceed further ! ; Return len_a which is greater or equal to len_b ; The main function that performs AND operation of two - bit sequences and returns the resultant string ; Make both strings of same length with the maximum length of s1 & s2 . ; Initialize res as NULL string ; We start from left to right as we have made both strings of same length . ; Convert s1 [ i ] and s2 [ i ] to int and perform bitwise AND operation , append to " res " string ; Driver code ; Check corner case : If there is just one binary string , then print it and return .
let s1 , s2 ; function makeEqualLength ( ) { let len_a = s1 . length ; let len_b = s2 . length ; let num_zeros = Math . abs ( len_a - len_b ) ; if ( len_a < len_b ) { for ( let i = 0 ; i < num_zeros ; i ++ ) { s1 = ' ' + s1 ; } return len_b ; } else { for ( let i = 0 ; i < num_zeros ; i ++ ) { s2 = ' ' + s2 ; } } return len_a ; } function andOperationBitwise ( ) { let length = makeEqualLength ( ) ; let res = " " ; for ( let i = 0 ; i < length ; i ++ ) { res = res + String . fromCharCode ( ( s1 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) & s2 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) + ' ' . charCodeAt ( ) ) ; } return res ; } let arr = [ " " , " " , " " ] ; let n = arr . length ; let result ; if ( n < 2 ) { document . write ( arr [ n - 1 ] + " " ) ; } result = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { s1 = result ; s2 = arr [ i ] ; result = andOperationBitwise ( ) ; } document . write ( result ) ;