Given two binary strings perform operation until B > 0 and print the result | Java 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
class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int BitOperations ( String a , int n , String b , int m ) { char [ ] ch1 = a . toCharArray ( ) ; reverse ( ch1 ) ; a = new String ( ch1 ) ; char [ ] ch2 = b . toCharArray ( ) ; reverse ( ch2 ) ; b = new String ( ch2 ) ; int c = 0 ; for ( int i = 0 ; i < m ; i ++ ) if ( b . charAt ( 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 . charAt ( i ) == '1' ) { ans += c * power [ i ] ; if ( ans >= mod ) ans %= mod ; } if ( b . charAt ( i ) == '1' ) c -- ; if ( c == 0 ) break ; } return ans ; } static void reverse ( char a [ ] ) { int i , k , n = a . length ; char t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } public static void main ( String [ ] args ) { String a = "1001" , b = "10101" ; int n = a . length ( ) , m = b . length ( ) ; System . out . println ( BitOperations ( a , n , b , m ) ) ; } }