Bitwise AND of N binary strings | this function takes 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 .
def makeEqualLength ( a , b ) : NEW_LINE INDENT len_a = len ( a ) NEW_LINE len_b = len ( b ) NEW_LINE num_zeros = abs ( len_a - len_b ) NEW_LINE if ( len_a < len_b ) : NEW_LINE INDENT for i in range ( num_zeros ) : NEW_LINE INDENT a = '0' + a NEW_LINE DEDENT return len_b , a , b NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( num_zeros ) : NEW_LINE INDENT b = '0' + b NEW_LINE DEDENT DEDENT return len_a , a , b NEW_LINE DEDENT def andOperationBitwise ( s1 , s2 ) : NEW_LINE INDENT length , s1 , s2 = makeEqualLength ( s1 , s2 ) NEW_LINE res = " " NEW_LINE for i in range ( length ) : NEW_LINE INDENT res = res + str ( int ( s1 [ i ] ) & int ( s2 [ i ] ) ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ "101" , "110110" , "111" ] NEW_LINE n = len ( arr ) NEW_LINE if ( n < 2 ) : NEW_LINE INDENT print ( arr [ n - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT result = arr [ 0 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT result = andOperationBitwise ( result , arr [ i ] ) ; NEW_LINE DEDENT print ( result ) NEW_LINE DEDENT