Given two binary strings A and B of length N and M (up to 105). The task is to repeat the below process and find the answer.
Initialize ans = 0
while (B > 0)
ans += A & B (bitwise AND)
B = B / 2
print ans
Note: Answer can be very large so print Answer % 1000000007.
Examples:
Input: A = "1001", B = "10101"
Output: 11
1001 & 10101 = 1, ans = 1, B = 1010
1001 & 1010 = 8, ans = 9, B = 101
1001 & 101 = 1, ans = 10, B = 10
1001 & 10 = 0, ans = 10, B = 1
1001 & 1 = 1, ans = 11, B = 0
Input: A = "1010", B = "1101"
Output: 12
Approach: Since only B is getting affected in all the iterations and dividing a binary number by 2 means right shifting it by 1 bit, it can be observed that a bit in A will only be affected by the set bits in B which are on the left i.e. more significant than the current bit (including the current bit). For example, A = “1001” and B = “10101”, the least significant bit in A will only be affected by the set bits in B i.e. 3 bits in total and the most significant bit in A will only be affected by a single set bit in B i.e. the most significant bit in B as all the other set bits will not affect it in any iteration of the loop while performing bitwise AND, so the final result will be 20 * 3 + 23 * 1 = 3 + 8 = 11.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod (int)(1e9 + 7)
ll BitOperations(string a, int n, string b, int m)
{
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int c = 0;
for ( int i = 0; i < m; i++)
if (b[i] == '1' )
c++;
ll power[n];
power[0] = 1;
for ( int i = 1; i < n; i++)
power[i] = (power[i - 1] * 2) % mod;
ll 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;
}
int main()
{
string a = "1001" , b = "10101" ;
int n = a.length(), m = b.length();
cout << BitOperations(a, n, b, m);
return 0;
}
|
Java
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));
}
}
|
Python3
mod = 1000000007
def BitOperations(a, n, b, m):
a = a[:: - 1 ]
b = b[:: - 1 ]
c = 0
for i in range (m):
if (b[i] = = '1' ):
c + = 1
power = [ None ] * n
power[ 0 ] = 1
for i in range ( 1 , n):
power[i] = (power[i - 1 ] * 2 ) % mod
ans = 0
for i in range ( 0 , n):
if (a[i] = = '1' ):
ans + = c * power[i]
if (ans > = mod):
ans % = mod
if (b[i] = = '1' ):
c - = 1
if (c = = 0 ):
break
return ans
if __name__ = = '__main__' :
a = "1001"
b = "10101"
n = len (a)
m = len (b)
print (BitOperations(a, n, b, m))
|
C#
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));
}
}
|
PHP
<?php
$GLOBALS [ 'mod' ] = (1e9 + 7);
function BitOperations( $a , $n , $b , $m )
{
$a = strrev ( $a );
$b = strrev ( $b );
$c = 0;
for ( $i = 0; $i < $m ; $i ++)
if ( $b [ $i ] == '1' )
$c ++;
# To store the powers of 2
$power = array () ;
$power [0] = 1;
for ( $i = 1; $i < $n ; $i ++)
$power [ $i ] = ( $power [ $i - 1] * 2) %
$GLOBALS [ 'mod' ];
$ans = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] == '1' )
{
$ans += $c * $power [ $i ];
if ( $ans >= $GLOBALS [ 'mod' ])
$ans %= $GLOBALS [ 'mod' ];
}
if ( $b [ $i ] == '1' )
$c --;
if ( $c == 0)
break ;
}
return $ans ;
}
$a = "1001" ;
$b = "10101" ;
$n = strlen ( $a );
$m = strlen ( $b );
echo BitOperations( $a , $n , $b , $m );
?>
|
Javascript
<script>
let mod = (1e9 + 7);
function BitOperations(a, n, b, m)
{
let ch1 = a.split( '' );
ch1.reverse();
a = ch1.join( "" );
let ch2 = b.split( '' );
ch2.reverse();
b = ch2.join( "" );
let c = 0;
for (let i = 0; i < m; i++)
if (b[i] == '1' )
c++;
let power = new Array(n);
power[0] = 1;
for (let i = 1; i < n; i++)
power[i] = (power[i - 1] * 2) % mod;
let ans = 0;
for (let 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;
}
let a = "1001" , b = "10101" ;
let n = a.length, m = b.length;
document.write(BitOperations(a, n, b, m));
</script>
|
Time Complexity: O(m + n)
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!