Given two binary strings perform operation until B > 0 and print the result | C # implementation of the approach ; Function to return the required result ; Reverse the strings ; Count the number of set bits in b ; To store the powers of 2 ; power [ i ] = pow ( 2 , i ) % mod ; To store the final answer ; Add power [ i ] to the ans after multiplying it with the number of set bits in b ; Divide by 2 means right shift b >> 1 if b has 1 at right most side than number of set bits will get decreased ; If no more set bits in b i . e . b = 0 ; Return the required answer ; Driver code
using System ; using System . Collections ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int BitOperations ( string a , int n , string b , int m ) { char [ ] ch1 = a . ToCharArray ( ) ; Array . Reverse ( ch1 ) ; a = new string ( ch1 ) ; char [ ] ch2 = b . ToCharArray ( ) ; Array . Reverse ( ch2 ) ; b = new string ( ch2 ) ; int c = 0 ; for ( int i = 0 ; i < m ; i ++ ) if ( b [ i ] == '1' ) c ++ ; int [ ] power = new int [ n ] ; power [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) power [ i ] = ( power [ i - 1 ] * 2 ) % mod ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == '1' ) { ans += c * power [ i ] ; if ( ans >= mod ) ans %= mod ; } if ( b [ i ] == '1' ) c -- ; if ( c == 0 ) break ; } return ans ; } static void Main ( ) { string a = "1001" , b = "10101" ; int n = a . Length , m = b . Length ; Console . WriteLine ( BitOperations ( a , n , b , m ) ) ; } }