Bitwise AND of N Binary Strings in C++



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]

Example

Let's take an example to understand the problem.

  • Input ? bin[] = {"1001", "11001", "010101"}
  • Output ? 000001

Explanation

Bitwise AND of all binary strings ?

(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.

Algorithm

The 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

Program

Program to illustrate the above solution,

#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;
  }
  return 0;
}

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.

Approach 2

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

Example

Let's take a sample example to make the solution clear,

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

Program

Program to show the implementation of the above approach ?

#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;
  }
  return 0;
}

Output

000001
Updated on: 2024-12-03T22:07:15+05:30

673 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements