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 ; Utility reversefunction ; Swap values of left and right ; Driver code
using System ; class GfG { static String strBitwiseAND ( String [ ] arr , int n ) { String res = " " ; int smallest_size = int . MaxValue ; int largest_size = int . MinValue ; String temp = " " ; for ( int i = 0 ; i < n ; i ++ ) { temp += arr [ i ] ; arr [ i ] = reverse ( temp ) ; smallest_size = Math . Min ( smallest_size , arr [ i ] . Length ) ; largest_size = Math . Max ( largest_size , arr [ i ] . Length ) ; } 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' ; String temp1 = " " ; temp1 += res ; res = reverse ( temp1 ) ; return res ; } static String reverse ( String input ) { char [ ] temparray = input . ToCharArray ( ) ; int left , right = 0 ; right = temparray . Length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . Join ( " " , temparray ) ; } public static void Main ( String [ ] args ) { String [ ] arr = { "101" , "110110" , "111" } ; int n = arr . Length ; Console . WriteLine ( strBitwiseAND ( arr , n ) ) ; } }