Bitwise AND of N binary strings | C ++ implementation of the above approach ; Function to find the bitwise AND of all the binary strings ; To store the largest and the smallest string ' s ▁ size , ▁ We ▁ need ▁ this ▁ to ▁ add ▁ ' 0 's  in the resultant string ; Reverse each string Since we need to perform AND operation on bits from Right to Left ; Update the respective length values ; Traverse bits from 0 to smallest string 's size ; If at this bit position , there is a 0 in any of the given strings then AND operation on current bit position will be 0 ; Add resultant bit to result ; Add 0 's to the string. ; Reverse the string Since we started from LEFT to RIGHT ; Return the resultant string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string strBitwiseAND ( string * arr , int n ) { string res ; int smallest_size = INT_MAX ; int largest_size = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { reverse ( arr [ i ] . begin ( ) , arr [ i ] . end ( ) ) ; smallest_size = min ( smallest_size , ( int ) arr [ i ] . size ( ) ) ; largest_size = max ( largest_size , ( int ) arr [ i ] . size ( ) ) ; } for ( int i = 0 ; i < smallest_size ; i ++ ) { bool all_ones = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] [ i ] == '0' ) { all_ones = false ; break ; } } res += ( all_ones ? '1' : '0' ) ; } for ( int i = 0 ; i < largest_size - smallest_size ; i ++ ) res += '0' ; reverse ( res . begin ( ) , res . end ( ) ) ; return res ; } int main ( ) { string arr [ ] = { "101" , "110110" , "111" } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << strBitwiseAND ( arr , n ) ; return 0 ; }