Bitwise AND of N binary strings in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an array bin[] of size n of binary strings. Our task is to create a program to find the Bitwise AND (&) of N binary strings.

Here, we will take all numbers and find the bitwise AND of them i.e. bin[0] & bin[1] &... bin[n-2] & bin[n]

Let’s take an example to understand the problem,

Input

bin[] = {“1001”, “11001”, “010101”}

Output

000001

Explanation − Bitwise AND of all binary string −

(1001) & (11001) & (010101) = 000001

To solve this problem, a direct and simple approach is to find the bitwise AND of two binary strings and then find the bitwise AND of the result with the next and go on till the last string of the array.

Basic Algorithm will be −

initially → result = bin[0] and i = 1

Step 1 − Repeat steps 2 and 3 until the array ends.

Step 2 − result = result & bin[i]

Step 3 − i++;

Step 4 − print the result.

Now, let’s solve the example with the help of this approach −

bin[] = {“1001”, “11001”, “010101”}
result = bin[0] = 1001, i = 1

Iteration 1

result = 1001 & 11001 = 01001
i = 2

Iteration 2

result = 01001 & 010101 = 000001
i = 3. END

Example

Program to illustrate the above solution,

 Live Demo

#include <iostream>
using namespace std;
int changeLength(string &a, string &b){
   int lengtha = a.length();
   int lengthb = b.length();
   int zeros = abs(lengtha-lengthb);
   if (lengtha<lengthb) {
      for (int i = 0 ; i<zeros; i++)
      a = '0' + a;
      return lengthb;
   }
   else {
      for (int i = 0 ; i<zeros; i++)
      b = '0' + b;
   }
   return lengtha;
}
string bitwiseAND(string binary1, string binary2){
   int length = changeLength(binary1,binary2);
   string result = "";
   for (int i = 0 ; i<length; i++){
      result = result+(char)((binary1[i] - '0' & binary2[i]-'0')+'0');
   }
   return result;
}
int main(){
   string bin[] = {"1001", "11001", "010101"};
   int n = sizeof(bin)/sizeof(bin[0]);
   string result;
   if (n<2){
      cout<<bin[n-1]<<endl;
   }
   else{
      result = bin[0];
      for (int i = 1; i<n; i++)
      result = bitwiseAND(result, bin[i]);
      cout <<result<<endl;
   }
}

Output

000001

This approach is easy but not the most effective one as it needs to traverse the string.

Let’s discuss a more effective solution,

Here, we will find the size of the smallest and largest bits of the binary number. Then we will find the bitwise AND of each bit of the number and at the end we will add preceding 0’s (no. of zeros will be the largest - smallest).

Let’s take an sample example to make the solution clear,

bin[] = {"1001", "11001", "010101"}
Largest = 010101 smallest = 1001
010101 & 1001 = 00001

Example

Program to show the implementation of the above approach −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
string bitwiseANDarray(string* bin, int n){
   string result;
   int minSize = INT_MAX;
   int maxSize = INT_MIN;
   for (int i = 0; i < n; i++) {
      reverse(bin[i].begin(), bin[i].end());
      minSize = min(minSize, (int)bin[i].size());
      maxSize = max(maxSize, (int)bin[i].size());
   }
   for (int i = 0; i < minSize; i++) {
      bool setBit = true;
      for (int j = 0; j < n; j++) {
         if (bin[j][i] == '0') {
            setBit = false;
            break;
         }
      }
      result += (setBit ? '1' : '0');
   }
   for (int i = 0; i<abs(maxSize-minSize); i++)
   result += '0';
   reverse(result.begin(), result.end());
   return result;
}
int main(){
   string arr[] = {"1001", "11001", "010101"};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout<<bitwiseANDarray(arr, n);
   return 0;
}

Output

000001
raja
Published on 05-Aug-2020 07:51:05

Advertisements