Skip to main content

Find sum of all bitwise and pairs in an array

Here given code implementation process.

// C Program 
// Find sum of all bitwise and pairs in an array
#include <stdio.h>

//Display elements of given array
void printArray(int arr[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("  %d", arr[i]);
	}
	printf("\n");
}
// Calculate the sum of all bitwise And pairs in given array
void sumAndPair(int arr[], int size)
{
	// Loop controlling variables
	int i = 0;
	int j = 0;
	// Set the initial sum of zero
	int sum = 0;
	//Execute loop through by size
	for (i = 0; i < size; ++i)
	{
		// Perform And operation of pairs [i] and [j]
		for (j = i + 1; j < size; ++j)
		{
			// Add the result of And pair
			sum += arr[i] & arr[j];
		}
	}
	// Display array element
	printf(" Array element \n");
	printArray(arr, size);
	// Display calculated result
	printf(" Sum of all bitwise and pairs is : %d\n", sum);
}
int main(int argc, char
	const *argv[])
{
	// Define array of integer elements
	int arr[] = {
		2 , 3 , 0 , 1 , -3 , 4
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	/*
		(2 & 3) = 2
		(2 & 0) = 0
		(2 & 1) = 0
		(2 & -3) = 0
		(2 & 4) = 0
		(3 & 0) = 0
		(3 & 1) = 1
		(3 & -3) = 1
		(3 & 4) = 0
		(0 & 1) = 0
		(0 & -3) = 0
		(0 & 4) = 0
		(1 & -3) = 1
		(1 & 4) = 0
		(-3 & 4) = 4
		------------
		Sum      :  9
	*/
	sumAndPair(arr, size);
	return 0;
}

Output

 Array element
  2  3  0  1  -3  4
 Sum of all bitwise and pairs is : 9
/*
  Java Program for
  Find sum of all bitwise and pairs in an array
*/
class SumOperation
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("  " + arr[i]);
		}
		System.out.print("\n");
	}
	// Calculate the sum of all bitwise And pairs in given array
	public void sumAndPair(int[] arr)
	{
		// Get the size
		int size = arr.length;
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Set the initial sum of zero
		int sum = 0;
		//Execute loop through by size
		for (i = 0; i < size; ++i)
		{
			// Perform And operation of pairs [i] and [j]
			for (j = i + 1; j < size; ++j)
			{
				// Add the result of And pair
				sum += arr[i] & arr[j];
			}
		}
		// Display array element
		System.out.print(" Array element \n");
		printArray(arr, size);
		// Display calculated result
		System.out.print(" Sum of all bitwise and pairs is  :  " + sum + "\n");
	}
	public static void main(String[] args)
	{
		SumOperation task = new SumOperation();
		// Define array of integer elements
		int[] arr = {
			2 , 3 , 0 , 1 , -3 , 4
		};
		/*
		    (2 & 0) = 0
		    (2 & 1) = 0
		    (2 & -3) = 0
		    (2 & 4) = 0
		    (3 & 0) = 0
		    (3 & 1) = 1
		    (3 & -3) = 1
		    (3 & 4) = 0
		    (0 & 1) = 0
		    (0 & -3) = 0
		    (0 & 4) = 0
		    (1 & -3) = 1
		    (1 & 4) = 0
		    (-3 & 4) = 4
		    ------------
		    Sum      :  9
		*/
		task.sumAndPair(arr);
	}
}

Output

 Array element
  2  3  0  1  -3  4
 Sum of all bitwise and pairs is  :  9
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Find sum of all bitwise and pairs in an array
*/
class SumOperation
{
	public:
		//Display elements of given array
		void printArray(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "  " << arr[i];
			}
			cout << "\n";
		}
	// Calculate the sum of all bitwise And pairs in given array
	void sumAndPair(int arr[], int size)
	{
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Set the initial sum of zero
		int sum = 0;
		//Execute loop through by size
		for (i = 0; i < size; ++i)
		{
			// Perform And operation of pairs [i] and [j]
			for (j = i + 1; j < size; ++j)
			{
				// Add the result of And pair
				sum += arr[i] &arr[j];
			}
		}
		// Display array element
		cout << " Array element \n";
		this->printArray(arr, size);
		// Display calculated result
		cout << " Sum of all bitwise and pairs is  :  " << sum << "\n";
	}
};
int main()
{
	SumOperation task = SumOperation();
	// Define array of integer elements
	int arr[] = {
		2 , 3 , 0 , 1 , -3 , 4
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	/*
	    (2 & 0) = 0
	    (2 & 1) = 0
	    (2 & -3) = 0
	    (2 & 4) = 0
	    (3 & 0) = 0
	    (3 & 1) = 1
	    (3 & -3) = 1
	    (3 & 4) = 0
	    (0 & 1) = 0
	    (0 & -3) = 0
	    (0 & 4) = 0
	    (1 & -3) = 1
	    (1 & 4) = 0
	    (-3 & 4) = 4
	    ------------
	    Sum      :  9
	*/
	task.sumAndPair(arr, size);
	return 0;
}

Output

 Array element
  2  3  0  1  -3  4
 Sum of all bitwise and pairs is  :  9
// Include namespace system
using System;
/*
  C# Program for
  Find sum of all bitwise and pairs in an array
*/
public class SumOperation
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	// Calculate the sum of all bitwise And pairs in given array
	public void sumAndPair(int[] arr)
	{
		// Get the size
		int size = arr.Length;
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Set the initial sum of zero
		int sum = 0;
		//Execute loop through by size
		for (i = 0; i < size; ++i)
		{
			// Perform And operation of pairs [i] and [j]
			for (j = i + 1; j < size; ++j)
			{
				// Add the result of And pair
				sum += arr[i] & arr[j];
			}
		}
		// Display array element
		Console.Write(" Array element \n");
		printArray(arr, size);
		// Display calculated result
		Console.Write(" Sum of all bitwise and pairs is  :  " + sum + "\n");
	}
	public static void Main(String[] args)
	{
		SumOperation task = new SumOperation();
		// Define array of integer elements
		int[] arr = {
			2 , 3 , 0 , 1 , -3 , 4
		};
		/*
		    (2 & 0) = 0
		    (2 & 1) = 0
		    (2 & -3) = 0
		    (2 & 4) = 0
		    (3 & 0) = 0
		    (3 & 1) = 1
		    (3 & -3) = 1
		    (3 & 4) = 0
		    (0 & 1) = 0
		    (0 & -3) = 0
		    (0 & 4) = 0
		    (1 & -3) = 1
		    (1 & 4) = 0
		    (-3 & 4) = 4
		    ------------
		    Sum      :  9
		*/
		task.sumAndPair(arr);
	}
}

Output

 Array element
  2  3  0  1  -3  4
 Sum of all bitwise and pairs is  :  9
<?php
/*
  Php Program for
  Find sum of all bitwise and pairs in an array
*/
class SumOperation
{
	//Display elements of given array
	public	function printArray( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	// Calculate the sum of all bitwise And pairs in given array
	public	function sumAndPair( & $arr)
	{
		// Get the size
		$size = count($arr);
		// Loop controlling variables
		$i = 0;
		$j = 0;
		// Set the initial sum of zero
		$sum = 0;
		//Execute loop through by size
		for ($i = 0; $i < $size; ++$i)
		{
			// Perform And operation of pairs [i] and [j]
			for ($j = $i + 1; $j < $size; ++$j)
			{
				// Add the result of And pair
				$sum += $arr[$i] & $arr[$j];
			}
		}
		// Display array element
		echo " Array element \n";
		$this->printArray($arr, $size);
		// Display calculated result
		echo " Sum of all bitwise and pairs is  :  ". $sum ."\n";
	}
}

function main()
{
	$task = new SumOperation();
	// Define array of integer elements
	$arr = array(2, 3, 0, 1, -3, 4);
	/*
	    (2 & 0) = 0
	    (2 & 1) = 0
	    (2 & -3) = 0
	    (2 & 4) = 0
	    (3 & 0) = 0
	    (3 & 1) = 1
	    (3 & -3) = 1
	    (3 & 4) = 0
	    (0 & 1) = 0
	    (0 & -3) = 0
	    (0 & 4) = 0
	    (1 & -3) = 1
	    (1 & 4) = 0
	    (-3 & 4) = 4
	    ------------
	    Sum      :  9
	*/
	$task->sumAndPair($arr);
}
main();

Output

 Array element
  2  3  0  1  -3  4
 Sum of all bitwise and pairs is  :  9
/*
  Node Js Program for
  Find sum of all bitwise and pairs in an array
*/
class SumOperation
{
	//Display elements of given array
	printArray(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Calculate the sum of all bitwise And pairs in given array
	sumAndPair(arr)
	{
		// Get the size
		var size = arr.length;
		// Loop controlling variables
		var i = 0;
		var j = 0;
		// Set the initial sum of zero
		var sum = 0;
		//Execute loop through by size
		for (i = 0; i < size; ++i)
		{
			// Perform And operation of pairs [i] and [j]
			for (j = i + 1; j < size; ++j)
			{
				// Add the result of And pair
				sum += arr[i] & arr[j];
			}
		}
		// Display array element
		process.stdout.write(" Array element \n");
		this.printArray(arr, size);
		// Display calculated result
		process.stdout.write(" Sum of all bitwise and pairs is  :  " + sum + "\n");
	}
}

function main()
{
	var task = new SumOperation();
	// Define array of integer elements
	var arr = [2, 3, 0, 1, -3, 4];
	/*
	    (2 & 0) = 0
	    (2 & 1) = 0
	    (2 & -3) = 0
	    (2 & 4) = 0
	    (3 & 0) = 0
	    (3 & 1) = 1
	    (3 & -3) = 1
	    (3 & 4) = 0
	    (0 & 1) = 0
	    (0 & -3) = 0
	    (0 & 4) = 0
	    (1 & -3) = 1
	    (1 & 4) = 0
	    (-3 & 4) = 4
	    ------------
	    Sum      :  9
	*/
	task.sumAndPair(arr);
}
main();

Output

 Array element
  2  3  0  1  -3  4
 Sum of all bitwise and pairs is  :  9
#   Python 3 Program for
#   Find sum of all bitwise and pairs in an array

class SumOperation :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Calculate the sum of all bitwise And pairs in given array
	def sumAndPair(self, arr) :
		#  Get the size
		size = len(arr)
		#  Loop controlling variables
		i = 0
		j = 0
		#  Set the initial sum of zero
		sum = 0
		# Execute loop through by size
		while (i < size) :
			#  Perform And operation of pairs [i] and [j]
			j = i + 1
			while (j < size) :
				#  Add the result of And pair
				sum += arr[i] & arr[j]
				j += 1
			
			i += 1
		
		#  Display array element
		print(" Array element ")
		self.printArray(arr, size)
		#  Display calculated result
		print(" Sum of all bitwise and pairs is  :  ", sum )
	

def main() :
	task = SumOperation()
	#  Define array of integer elements
	arr = [2, 3, 0, 1, -3, 4]
	# 
	#     (2 & 0) = 0
	#     (2 & 1) = 0
	#     (2 & -3) = 0
	#     (2 & 4) = 0
	#     (3 & 0) = 0
	#     (3 & 1) = 1
	#     (3 & -3) = 1
	#     (3 & 4) = 0
	#     (0 & 1) = 0
	#     (0 & -3) = 0
	#     (0 & 4) = 0
	#     (1 & -3) = 1
	#     (1 & 4) = 0
	#     (-3 & 4) = 4
	#     ------------
	#     Sum      :  9
	
	task.sumAndPair(arr)

if __name__ == "__main__": main()

Output

 Array element
   2   3   0   1   -3   4
 Sum of all bitwise and pairs is  :   9
#   Ruby Program for
#   Find sum of all bitwise and pairs in an array

class SumOperation 
	# Display elements of given array
	def printArray(arr, size) 
		i = 0
		while (i < size) 
			print("  ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Calculate the sum of all bitwise And pairs in given array
	def sumAndPair(arr) 
		#  Get the size
		size = arr.length
		#  Loop controlling variables
		i = 0
		j = 0
		#  Set the initial sum of zero
		sum = 0
		# Execute loop through by size
		while (i < size) 
			#  Perform And operation of pairs [i] and [j]
			j = i + 1
			while (j < size) 
				#  Add the result of And pair
				sum += arr[i] & arr[j]
				j += 1
			end

			i += 1
		end

		#  Display array element
		print(" Array element \n")
		self.printArray(arr, size)
		#  Display calculated result
		print(" Sum of all bitwise and pairs is  :  ", sum ,"\n")
	end

end

def main() 
	task = SumOperation.new()
	#  Define array of integer elements
	arr = [2, 3, 0, 1, -3, 4]
	# 
	#     (2 & 0) = 0
	#     (2 & 1) = 0
	#     (2 & -3) = 0
	#     (2 & 4) = 0
	#     (3 & 0) = 0
	#     (3 & 1) = 1
	#     (3 & -3) = 1
	#     (3 & 4) = 0
	#     (0 & 1) = 0
	#     (0 & -3) = 0
	#     (0 & 4) = 0
	#     (1 & -3) = 1
	#     (1 & 4) = 0
	#     (-3 & 4) = 4
	#     ------------
	#     Sum      :  9
	
	task.sumAndPair(arr)
end

main()

Output

 Array element 
  2  3  0  1  -3  4
 Sum of all bitwise and pairs is  :  9
/*
  Scala Program for
  Find sum of all bitwise and pairs in an array
*/
class SumOperation
{
	//Display elements of given array
	def printArray(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Calculate the sum of all bitwise And pairs in given array
	def sumAndPair(arr: Array[Int]): Unit = {
		// Get the size
		var size: Int = arr.length;
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// Set the initial sum of zero
		var sum: Int = 0;
		//Execute loop through by size
		while (i < size)
		{
			// Perform And operation of pairs [i] and [j]
			j = i + 1;
			while (j < size)
			{
				// Add the result of And pair
				sum += arr(i) & arr(j);
				j += 1;
			}
			i += 1;
		}
		// Display array element
		print(" Array element \n");
		this.printArray(arr, size);
		// Display calculated result
		print(" Sum of all bitwise and pairs is  :  " + sum + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: SumOperation = new SumOperation();
		// Define array of integer elements
		var arr: Array[Int] = Array(2, 3, 0, 1, -3, 4);
		/*
		    (2 & 0) = 0
		    (2 & 1) = 0
		    (2 & -3) = 0
		    (2 & 4) = 0
		    (3 & 0) = 0
		    (3 & 1) = 1
		    (3 & -3) = 1
		    (3 & 4) = 0
		    (0 & 1) = 0
		    (0 & -3) = 0
		    (0 & 4) = 0
		    (1 & -3) = 1
		    (1 & 4) = 0
		    (-3 & 4) = 4
		    ------------
		    Sum      :  9
		*/
		task.sumAndPair(arr);
	}
}

Output

 Array element
  2  3  0  1  -3  4
 Sum of all bitwise and pairs is  :  9
/*
  Swift 4 Program for
  Find sum of all bitwise and pairs in an array
*/
class SumOperation
{
	//Display elements of given array
	func printArray(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Calculate the sum of all bitwise And pairs in given array
	func sumAndPair(_ arr: [Int])
	{
		// Get the size
		let size: Int = arr.count;
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// Set the initial sum of zero
		var sum: Int = 0;
		//Execute loop through by size
		while (i < size)
		{
			// Perform And operation of pairs [i] and [j]
			j = i + 1;
			while (j < size)
			{
				// Add the result of And pair
				sum += arr[i] & arr[j];
				j += 1;
			}
			i += 1;
		}
		// Display array element
		print(" Array element ");
		self.printArray(arr, size);
		// Display calculated result
		print(" Sum of all bitwise and pairs is  :  ", sum );
	}
}
func main()
{
	let task: SumOperation = SumOperation();
	// Define array of integer elements
	let arr: [Int] = [2, 3, 0, 1, -3, 4];
	/*
	    (2 & 0) = 0
	    (2 & 1) = 0
	    (2 & -3) = 0
	    (2 & 4) = 0
	    (3 & 0) = 0
	    (3 & 1) = 1
	    (3 & -3) = 1
	    (3 & 4) = 0
	    (0 & 1) = 0
	    (0 & -3) = 0
	    (0 & 4) = 0
	    (1 & -3) = 1
	    (1 & 4) = 0
	    (-3 & 4) = 4
	    ------------
	    Sum      :  9
	*/
	task.sumAndPair(arr);
}
main();

Output

 Array element
   2   3   0   1   -3   4
 Sum of all bitwise and pairs is  :   9
/*
  Kotlin Program for
  Find sum of all bitwise and pairs in an array
*/
class SumOperation
{
	//Display elements of given array
	fun printArray(arr: Array < Int > , size: Int): Unit
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Calculate the sum of all bitwise And pairs in given array
	fun sumAndPair(arr: Array < Int > ): Unit
	{
		// Get the size
		var size: Int = arr.count();
		// Loop controlling variables
		var i: Int = 0;
		var j: Int ;
		// Set the initial sum of zero
		var sum: Int = 0;
		//Execute loop through by size
		while (i < size)
		{
			// Perform And operation of pairs [i] and [j]
			j = i + 1;
			while (j < size)
			{
				// Add the result of And pair
				sum += arr[i] and arr[j];
				j += 1;
			}
			i += 1;
		}
		// Display array element
		print(" Array element \n");
		this.printArray(arr, size);
		// Display calculated result
		print(" Sum of all bitwise and pairs is  :  " + sum + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: SumOperation = SumOperation();
	// Define array of integer elements
	var arr: Array < Int > = arrayOf(2, 3, 0, 1, -3, 4);
	/*
	    (2 & 0) = 0
	    (2 & 1) = 0
	    (2 & -3) = 0
	    (2 & 4) = 0
	    (3 & 0) = 0
	    (3 & 1) = 1
	    (3 & -3) = 1
	    (3 & 4) = 0
	    (0 & 1) = 0
	    (0 & -3) = 0
	    (0 & 4) = 0
	    (1 & -3) = 1
	    (1 & 4) = 0
	    (-3 & 4) = 4
	    ------------
	    Sum      :  9
	*/
	task.sumAndPair(arr);
}

Output

 Array element
  2  3  0  1  -3  4
 Sum of all bitwise and pairs is  :  9




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