Skip to main content

Perform Bitwise XOR in a Binary array

Here given code implementation process.

// C Program
// Perform Bitwise XOR in a Binary array
#include <stdio.h>
#include <string.h>

// Returns the length of maximum word 
int maxLength(const char *
    const arr[], int size)
{
    int length = strlen(arr[0]);

    for (int i = 1; i < size; ++i)
    {
        if (length < strlen(arr[i]))
        {
            length = strlen(arr[i]);
            
        }
    }
    return length;
}
// Perform Bitwise XOR
void bitwiseXor(const char *const arr[], int size)
{

    //Get the length of largest binary word in given array
    int length =  maxLength(arr, size);
    int bits = 0;
    int l = 0;
    // Execute loop under the length of max word
    for (int i = length - 1; i >= 0; --i)
    {
        bits = -1;
        for (int j = 0; j < size; ++j)
        {
            // Get length 
            l = strlen(arr[j]) - 1;
            // Check if word contains column bit
            if (l >= i)
            {
                // Get actual start position 
                l = l - i;
                if (bits == -1)
                {
                    // Get get a first top bit of current column
                    bits = (arr[j][l] - '0');
                }
                else
                {
                    // Perform Xor operation in two bits
                    bits = bits ^ (arr[j][l] - '0');
                }
            }
        }
        printf("%d", bits);
    }
    printf("\n");
}
int main()
{
    //Define the array of binary string
    const char * const arr[] = 
    {
       "111" , "1001" , "101010" , "10011" , "11100"
    };
    // Get the size
    int size = sizeof(arr) / sizeof(arr[0]);
    /*
    ------------
          1 1 1
        1 0 0 1
    1 0 1 0 1 0
      1 0 0 1 1
      1 1 1 0 0
    ------------
    1 0 1 0 1 1
    -----------
    */
    bitwiseXor(arr, size);
    return 0;
}

Output

101011
/*
  Java Program
  Perform Bitwise XOR in a Binary array
*/

public class BitwiseOperation 
{

    // Returns the length of maximum word 
    public int maxLength(String []arr, int size)
    {
        int length = arr[0].length();
        for (int i = 1; i < size; ++i)
        {
            if (length < arr[i].length())
            {
                length = arr[i].length();
            }
        }
        return length;
    }
    // Perform Bitwise XOR
    public void bitwiseXor(String []arr, int size)
    {
        //Get the length of largest binary word in given array
        int length = maxLength(arr, size);
        int bits = 0;
        int l = 0;
        // Execute loop under the length of max word
        for (int i = length - 1; i >= 0; --i)
        {
            bits = -1;
            for (int j = 0; j < size; ++j)
            {
                // Get length 
                l = arr[j].length() - 1;
                // Check if word contains column bit
                if (l >= i)
                {
                    // Get actual start position 
                    l = l - i;
                    if (bits == -1)
                    {
                        // Get get a first top bit of current column
                        bits = arr[j].charAt(l) - '0';
                    }
                    else
                    {
                        // Perform Xor operation in two bits
                        bits = bits ^ (arr[j].charAt(l) - '0');
                    }
                }
            }
            System.out.print(bits);
        }
        System.out.print("\n");
    }
  public static void main(String[] args) {

    BitwiseOperation op = new BitwiseOperation();
    //Define the array of binary string
    String [] arr = {
        "111" , "1001" , "101010" , "10011" , "11100"
    };
    // Get the size
    int size = arr.length;
    /*
        ------------
              1 1 1
            1 0 0 1
        1 0 1 0 1 0
          1 0 0 1 1
          1 1 1 0 0
        ------------
        1 0 1 0 1 1
        -----------
        */
    op.bitwiseXor(arr, size);
 
  }
}

Output

101011
// Include header file
#include <iostream>
#include <string.h>
using namespace std;
/*
  C++ Program
  Perform Bitwise XOR in a Binary array
*/
class BitwiseOperation
{
    public:
    //  Returns the length of maximum word
    int maxLength(const char * const arr[], int size)
    {
        int length = strlen(arr[0]);
        for (int i = 1; i < size; ++i)
        {
            if (length < strlen(arr[i]))
            {
                length = strlen(arr[i]);
            }
        }
        return length;
    }
    //  Perform Bitwise XOR
    void bitwiseXor(const char * const arr[], int size)
    {
        // Get the length of largest binary word in given array
        int length = this->maxLength(arr, size);
        int bits = 0;
        int l = 0;
        //  Execute loop under the length of max word
        for (int i = length - 1; i >= 0; --i)
        {
            bits = -1;
            for (int j = 0; j < size; ++j)
            {
                //  Get length
                l = strlen(arr[j]) - 1;
                //  Check if word contains column bit
                if (l >= i)
                {
                    //  Get actual start position
                    l = l - i;
                    if (bits == -1)
                    {
                        //  Get get a first top bit of current column
                        bits = arr[j][l] - '0';
                    }
                    else
                    {
                        //  Perform Xor operation in two bits
                        bits = bits ^ (arr[j][l] - '0');
                    }
                }
            }
            cout << bits;
        }
        cout << "\n";
    }
};
int main()
{
    BitwiseOperation op = BitwiseOperation();
    // Define the array of binary string
    const char * const arr[] = {
        "111" , "1001" , "101010" , "10011" , "11100"
    };
    //  Get the size
    int size = sizeof(arr) / sizeof(arr[0]);
    /*
                ------------
                      1 1 1
                    1 0 0 1
                1 0 1 0 1 0
                  1 0 0 1 1
                  1 1 1 0 0
                ------------
                1 0 1 0 1 1
                -----------
                */
    op.bitwiseXor(arr, size);
    return 0;
}

Output

101011
// Include namespace system
using System;
/*
  C# Program
  Perform Bitwise XOR in a Binary array
*/
public class BitwiseOperation
{
	//  Returns the length of maximum word
	public int maxLength(String[] arr, int size)
	{
		int length = arr[0].Length;
		for (int i = 1; i < size; ++i)
		{
			if (length < arr[i].Length)
			{
				length = arr[i].Length;
			}
		}
		return length;
	}
	//  Perform Bitwise XOR
	public void bitwiseXor(String[] arr, int size)
	{
		// Get the length of largest binary word in given array
		int length = maxLength(arr, size);
		int bits = 0;
		int l = 0;
		//  Execute loop under the length of max word
		for (int i = length - 1; i >= 0; --i)
		{
			bits = -1;
			for (int j = 0; j < size; ++j)
			{
				//  Get length
				l = arr[j].Length - 1;
				//  Check if word contains column bit
				if (l >= i)
				{
					//  Get actual start position
					l = l - i;
					if (bits == -1)
					{
						//  Get get a first top bit of current column
						bits = arr[j][l] - '0';
					}
					else
					{
						//  Perform Xor operation in two bits
						bits = bits ^ (arr[j][l] - '0');
					}
				}
			}
			Console.Write(bits);
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		BitwiseOperation op = new BitwiseOperation();
		// Define the array of binary string
		String[] arr = {
			"111" , "1001" , "101010" , "10011" , "11100"
		};
		//  Get the size
		int size = arr.Length;
        /*
        ------------
              1 1 1
            1 0 0 1
        1 0 1 0 1 0
          1 0 0 1 1
          1 1 1 0 0
        ------------
        1 0 1 0 1 1
        -----------
        */
		op.bitwiseXor(arr, size);
	}
}

Output

101011
<?php
/*
  Php Program
  Perform Bitwise XOR in a Binary array
*/
class BitwiseOperation
{
	//  Returns the length of maximum word
	public	function maxLength( & $arr, $size)
	{
		$length = strlen($arr[0]);
		for ($i = 1; $i < $size; ++$i)
		{
			if ($length < strlen($arr[$i]))
			{
				$length = strlen($arr[$i]);
			}
		}
		return $length;
	}
	//  Perform Bitwise XOR
	public	function bitwiseXor( & $arr, $size)
	{
		// Get the length of largest binary word in given array
		$length = $this->maxLength($arr, $size);
		$bits = 0;
		$l = 0;
		//  Execute loop under the length of max word
		for ($i = $length - 1; $i >= 0; --$i)
		{
			$bits = -1;
			for ($j = 0; $j < $size; ++$j)
			{
				//  Get length
				$l =  strlen($arr[$j]) - 1;
				//  Check if word contains column bit
				if ($l >= $i)
				{
					//  Get actual start position
					$l = $l - $i;
					if ($bits == -1)
					{
						//  Get get a first top bit of current column
						$bits = ord($arr[$j][$l]) - ord('0');
					}
					else
					{
						//  Perform Xor operation in two bits
						$bits = $bits ^ (ord($arr[$j][$l]) - ord('0'));
					}
				}
			}
			echo $bits;
		}
		echo "\n";
	}
}

function main()
{
	$op = new BitwiseOperation();
	// Define the array of binary string
	$arr = array("111", "1001", "101010", "10011", "11100");
	//  Get the size
	$size = count($arr);
	/*
	------------
	      1 1 1
	    1 0 0 1
	1 0 1 0 1 0
	  1 0 0 1 1
	  1 1 1 0 0
	------------
	1 0 1 0 1 1
	-----------
	*/
	$op->bitwiseXor($arr, $size);
}
main();

Output

101011
/*
  Node Js Program
  Perform Bitwise XOR in a Binary array
*/
class BitwiseOperation
{
	//  Returns the length of maximum word
	maxLength(arr, size)
	{
		var length = arr[0].length;
		for (var i = 1; i < size; ++i)
		{
			if (length < arr[i].length)
			{
				length = arr[i].length;
			}
		}
		return length;
	}
	//  Perform Bitwise XOR
	bitwiseXor(arr, size)
	{
		// Get the length of largest binary word in given array
		var length = this.maxLength(arr, size);
		var bits = 0;
		var l = 0;
		//  Execute loop under the length of max word
		for (var i = length - 1; i >= 0; --i)
		{
			bits = -1;
			for (var j = 0; j < size; ++j)
			{
				//  Get length
				l = arr[j].length - 1;
				//  Check if word contains column bit
				if (l >= i)
				{
					//  Get actual start position
					l = l - i;
					if (bits == -1)
					{
						//  Get get a first top bit of current column
						bits = arr[j].charCodeAt(l) - ('0').charCodeAt(0);
					}
					else
					{
						//  Perform Xor operation in two bits
						bits = bits ^ (arr[j].charCodeAt(l) - ('0').charCodeAt(0));
					}
				}
			}
			process.stdout.write(""+bits);
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var op = new BitwiseOperation();
	// Define the array of binary string
	var arr = ["111", "1001", "101010", "10011", "11100"];
	//  Get the size
	var size = arr.length;
	/*
	------------
	      1 1 1
	    1 0 0 1
	1 0 1 0 1 0
	  1 0 0 1 1
	  1 1 1 0 0
	------------
	1 0 1 0 1 1
	-----------
	*/
	op.bitwiseXor(arr, size);
}
main();

Output

101011
#   Python 3 Program
#   Perform Bitwise XOR in a Binary array

class BitwiseOperation :
	#   Returns the length of maximum word
	def maxLength(self, arr, size) :
		length = len(arr[0])
		i = 1
		while (i < size) :
			if (length < len(arr[i])) :
				length = len(arr[i])
			
			i += 1
		
		return length
	
	#   Perform Bitwise XOR
	def bitwiseXor(self, arr, size) :
		#  Get the length of largest binary word in given array
		length = self.maxLength(arr, size)
		bits = 0
		l = 0
		#   Execute loop under the length of max word
		i = length - 1
		while (i >= 0) :
			bits = -1
			j = 0
			while (j < size) :
				#   Get length
				l = len(arr[j]) - 1
				#   Check if word contains column bit
				if (l >= i) :
					#   Get actual start position
					l = l - i
					if (bits == -1) :
						#   Get get a first top bit of current column
						bits = ord(arr[j][l]) - ord('0')
					else :
						#   Perform Xor operation in two bits
						bits = bits ^ (ord(arr[j][l]) - ord('0'))
					
				
				j += 1
			
			print(bits, end = "")
			i -= 1
		
		print(end = "\n")
	

def main() :
	op = BitwiseOperation()
	#  Define the array of binary string
	arr = ["111", "1001", "101010", "10011", "11100"]
	#   Get the size
	size = len(arr)
	# 
	# ------------
	#       1 1 1
	#     1 0 0 1
	# 1 0 1 0 1 0
	#   1 0 0 1 1
	#   1 1 1 0 0
	# ------------
	# 1 0 1 0 1 1
	# -----------
	
	op.bitwiseXor(arr, size)

if __name__ == "__main__": main()

Output

101011
#  Ruby Program
#  Perform Bitwise XOR in a Binary array

class BitwiseOperation 
	#   Returns the length of maximum word
	def maxLength(arr, size) 
		length = arr[0].length()
		i = 1
		while (i < size) 
			if (length < arr[i].length()) 
				length = arr[i].length()
			end

			i += 1
		end

		return length
	end

	#   Perform Bitwise XOR
	def bitwiseXor(arr, size) 
		#  Get the length of largest binary word in given array
		length = self.maxLength(arr, size)
		bits = 0
		l = 0
		#   Execute loop under the length of max word
		i = length - 1
		while (i >= 0) 
			bits = -1
			j = 0
			while (j < size) 
				#   Get length
				l = arr[j].length() - 1
				#   Check if word contains column bit
				if (l >= i) 
					#   Get actual start position
					l = l - i
					if (bits == -1) 
						#   Get get a first top bit of current column
						bits = (arr[j][l]).ord - ('0').ord
					else 
						#   Perform Xor operation in two bits
						bits = bits ^ ((arr[j][l]).ord - ('0').ord)
					end

				end

				j += 1
			end

			print(bits)
			i -= 1
		end

		print("\n")
	end

end

def main() 
	op = BitwiseOperation.new()
	#  Define the array of binary string
	arr = ["111", "1001", "101010", "10011", "11100"]
	#   Get the size
	size = arr.length
	# 
	# ------------
	#       1 1 1
	#     1 0 0 1
	# 1 0 1 0 1 0
	#   1 0 0 1 1
	#   1 1 1 0 0
	# ------------
	# 1 0 1 0 1 1
	# -----------
	
	op.bitwiseXor(arr, size)
end

main()

Output

101011
/*
  Scala Program
  Perform Bitwise XOR in a Binary array
*/
class BitwiseOperation
{
	//   Returns the length of maximum word
	def maxLength(arr: Array[String], size: Int): Int = {
		var length: Int = arr(0).length();
		var i: Int = 1;
		while (i < size)
		{
			if (length < arr(i).length())
			{
				length = arr(i).length();
			}
			i += 1;
		}
		return length;
	}
	//   Perform Bitwise XOR
	def bitwiseXor(arr: Array[String], size: Int): Unit = {
		//  Get the length of largest binary word in given array
		var length: Int = this.maxLength(arr, size);
		var bits: Int = 0;
		var l: Int = 0;
		//   Execute loop under the length of max word
		var i: Int = length - 1;
		while (i >= 0)
		{
			bits = -1;
			var j: Int = 0;
			while (j < size)
			{
				//   Get length
				l = arr(j).length() - 1;
				//   Check if word contains column bit
				if (l >= i)
				{
					//   Get actual start position
					l = l - i;
					if (bits == -1)
					{
						//   Get get a first top bit of current column
						bits = arr(j)(l) - '0';
					}
					else
					{
						//   Perform Xor operation in two bits
						bits = bits ^ (arr(j)(l) - '0');
					}
				}
				j += 1;
			}
			print(bits);
			i -= 1;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var op: BitwiseOperation = new BitwiseOperation();
		//  Define the array of binary string
		var arr: Array[String] = Array("111", "1001", "101010", "10011", "11100");
		//   Get the size
		var size: Int = arr.length;
		/*
		------------
		      1 1 1
		    1 0 0 1
		1 0 1 0 1 0
		  1 0 0 1 1
		  1 1 1 0 0
		------------
		1 0 1 0 1 1
		-----------
		*/
		op.bitwiseXor(arr, size);
	}
}

Output

101011
/*
  Swift 4 Program
  Perform Bitwise XOR in a Binary array
*/
class BitwiseOperation
{
    // Returns the length of maximum word
    func maxLength(_ arr: [String], _ size: Int)->Int
    {
        var length: Int = arr[0].count;
        var i: Int = 1;
        while (i < size)
        {
            if (length < arr[i].count)
            {
                length = arr[i].count;
            }
            i += 1;
        }
        return length;
    }
    //   Perform Bitwise XOR
    func bitwiseXor(_ arr: [String], _ size: Int)
    {
        //  Get the length of largest binary word in given array
        let length: Int = self.maxLength(arr, size);
        var bits: Int = 0;
        var l: Int = 0;
        //   Execute loop under the length of max word
        var i: Int = length - 1;
        var ch: Character; 
        while (i >= 0)
        {
           
            bits = -1;
            var j: Int = 0;
            while (j < size)
            {
                // Get length
                l = arr[j].count - 1;
                // Check if word contains column bit
                if (l >= i)
                {
                             
                                                                                  
                    // Get actual start position
                    l = l - i;

                    ch = Array(arr[j])[l]; 
                    
                    if (bits == -1)
                    {
                        // Get get a first top bit of current column
                        bits = Int(UnicodeScalar(String(ch))!.value) - Int(UnicodeScalar("0")!.value);
                    }
                    else
                    {
                        // Perform Xor operation in two bits
                        bits = bits ^ Int(UnicodeScalar(String(ch))!.value) - Int(UnicodeScalar("0")!.value);
                    }
                }
                j += 1;
            }
            print(bits, terminator: "");
            i -= 1;
        }
        print(terminator: "\n");
    }
}
func main()
{
    let op: BitwiseOperation = BitwiseOperation();
    //  Define the array of binary string
    let arr: [String] = ["111", "1001", "101010", "10011", "11100"];
    //   Get the size
    let size: Int = arr.count;
    /*
    ------------
          1 1 1
        1 0 0 1
    1 0 1 0 1 0
      1 0 0 1 1
      1 1 1 0 0
    ------------
    1 0 1 0 1 1
    -----------
    */
    op.bitwiseXor(arr, size);
}
main();

Output

101011
/*
  Kotlin Program
  Perform Bitwise XOR in a Binary array
*/
class BitwiseOperation
{
	//   Returns the length of maximum word
	fun maxLength(arr: Array<String>, size: Int): Int
	{
		var length: Int = arr[0].length;
		var i: Int = 1;
		while (i<size)
		{
			if (length<arr[i].length)
			{
				length = arr[i].length;
			}
			i += 1;
		}
		return length;
	}
	//   Perform Bitwise XOR
	fun bitwiseXor(arr: Array<String>, size: Int): Unit
	{
		//  Get the length of largest binary word in given array
		var length: Int = this.maxLength(arr, size);
		var bits: Int ;
		var l: Int ;
      	var j: Int ;
		//   Execute loop under the length of max word
		var i: Int = length - 1;
		while (i>= 0)
		{
			bits = -1;
			j = 0;
			while (j<size)
			{
				// Get length
				l = arr[j].length - 1;
				//   Check if word contains column bit
				if (l>= i)
				{
                  
					// Get actual start position
					l = l - i;
                    var ch =  arr[j].get(l);
					if (bits == -1)
					{
						// Get get a first top bit of current column
						bits = ch.toInt() - '0'.toInt();
					}
					else
					{
						// Perform Xor operation in two bits
						bits =  bits xor ( ch.toInt() - '0'.toInt() ) ;
					}
				}
				j += 1;
			}
			print(bits);
			i -= 1;
		}
		print("\n");
	}
}
fun main(args: Array<String>): Unit
{
	var op: BitwiseOperation = BitwiseOperation();
	//  Define the array of binary string
	var arr: Array<String> = arrayOf("111", "1001", "101010", "10011", "11100");
	//   Get the size
	var size: Int = arr.count();
	/*
	------------
	      1 1 1
	    1 0 0 1
	1 0 1 0 1 0
	  1 0 0 1 1
	  1 1 1 0 0
	------------
	1 0 1 0 1 1
	-----------
	*/
	op.bitwiseXor(arr, size);
}

Output

101011




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment