Bitwise OR of N binary strings

Given an array arr[] of binary strings, the task is to calculate the bitwise OR of all of these strings and print the resultant string.

Examples:

Input: arr[] = {“100”, “1001”, “0011”}
Output 1111
0100 OR 1001 OR 0011 = 1111

Input: arr[] = {“10”, “11”, “1000001”}
Output: 1000011

Approach: We can do this by first finding the maximum sized string. We need this since we have to add 0s at the front of the strings whose lengths are less than max size. Then apply OR operation on each bit.
For example, if strings are “100”, “001” and “1111”. Here max size is 4, so we have to add 1 zero on first and second string to make their length 4 and then the OR operation can be performed on each of the bits of the numbers resulting in “0100” OR “0001” OR “1111” = “1111”.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the bitwise OR of
// all the binary strings
string strBitwiseOR(string* arr, int n)
{
    string res;
  
    int max_size = INT_MIN;
  
    // Get max size and reverse each string
    // Since we have to perform OR operation
    // on bits from right to left
    // Reversing the string will make it easier
    // to perform operation from left to right
    for (int i = 0; i < n; i++) {
        max_size = max(max_size, (int)arr[i].size());
        reverse(arr[i].begin(), arr[i].end());
    }
  
    for (int i = 0; i < n; i++) {
  
        // Add 0s to the end of strings
        // if needed
        string s;
        for (int j = 0; j < max_size - arr[i].size(); j++)
            s += '0';
  
        arr[i] = arr[i] + s;
    }
  
    // Perform OR operation on each bit
    for (int i = 0; i < max_size; i++) {
        int curr_bit = 0;
        for (int j = 0; j < n; j++)
            curr_bit = curr_bit | (arr[j][i] - '0');
  
        res += (curr_bit + '0');
    }
  
    // Reverse the resultant string
    // to get the final string
    reverse(res.begin(), res.end());
  
    // Return the final string
    return res;
}
  
// Driver code
int main()
{
    string arr[] = { "10", "11", "1000001" };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << strBitwiseOR(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
  
class GFG 
{
  
// Function to return the bitwise OR of
// all the binary strings
static String strBitwiseOR(String[] arr, int n)
{
    String res="";
  
    int max_size = Integer.MIN_VALUE;
  
    // Get max size and reverse each string
    // Since we have to perform OR operation
    // on bits from right to left
    // Reversing the string will make it easier
    // to perform operation from left to right
    for (int i = 0; i < n; i++)
    {
        max_size = Math.max(max_size, (int)arr[i].length());
        arr[i] = reverse(arr[i]);
    }
  
    for (int i = 0; i < n; i++) 
    {
  
        // Add 0s to the end of strings
        // if needed
        String s="";
        for (int j = 0; j < max_size - arr[i].length(); j++)
            s += '0';
  
        arr[i] = arr[i] + s;
    }
      
    // Perform OR operation on each bit
    for (int i = 0; i < max_size; i++)
    {
        int curr_bit = 0;
        for (int j = 0; j < n; j++)
            curr_bit = curr_bit | (arr[j].charAt(i) - '0');
  
        res += (char)(curr_bit + '0');
    }
  
    // Reverse the resultant string
    // to get the final string
    res = reverse(res);
  
    // Return the final string
    return res;
}
  
static String reverse(String input) 
{
    char[] temparray = input.toCharArray();
    int left, right = 0;
    right = temparray.length - 1;
  
    for (left = 0; left < right; left++, right--)
    {
        // Swap values of left and right 
        char temp = temparray[left];
        temparray[left] = temparray[right];
        temparray[right] = temp;
    }
    return String.valueOf(temparray);
      
// Driver code
public static void main(String[] args)
{
    String arr[] = { "10", "11", "1000001" };
    int n = arr.length;
    System.out.println(strBitwiseOR(arr, n));
}
}
  
// This code contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG 
{
   
// Function to return the bitwise OR of
// all the binary strings
static String strBitwiseOR(String[] arr, int n)
{
    String res="";
   
    int max_size = int.MinValue;
   
    // Get max size and reverse each string
    // Since we have to perform OR operation
    // on bits from right to left
    // Reversing the string will make it easier
    // to perform operation from left to right
    for (int i = 0; i < n; i++)
    {
        max_size = Math.Max(max_size, (int)arr[i].Length);
        arr[i] = reverse(arr[i]);
    }
   
    for (int i = 0; i < n; i++) 
    {
   
        // Add 0s to the end of strings
        // if needed
        String s="";
        for (int j = 0; j < max_size - arr[i].Length; j++)
            s += '0';
   
        arr[i] = arr[i] + s;
    }
       
    // Perform OR operation on each bit
    for (int i = 0; i < max_size; i++)
    {
        int curr_bit = 0;
        for (int j = 0; j < n; j++)
            curr_bit = curr_bit | (arr[j][i] - '0');
   
        res += (char)(curr_bit + '0');
    }
   
    // Reverse the resultant string
    // to get the final string
    res = reverse(res);
   
    // Return the final string
    return res;
}
   
static String reverse(String input) 
{
    char[] temparray = input.ToCharArray();
    int left, right = 0;
    right = temparray.Length - 1;
   
    for (left = 0; left < right; left++, right--)
    {
        // Swap values of left and right 
        char temp = temparray[left];
        temparray[left] = temparray[right];
        temparray[right] = temp;
    }
    return String.Join("",temparray);
       
// Driver code
public static void Main(String[] args)
{
    String []arr = { "10", "11", "1000001" };
    int n = arr.Length;
    Console.WriteLine(strBitwiseOR(arr, n));
}
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


Output:

1000011


My Personal Notes arrow_drop_up

A Coding Enthusiast Rails Developer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji, princiraj1992