# Sum of bit differences among all pairs

Here given code implementation process.

``````// C Program
// Sum of bit differences among all pairs
#include <stdio.h>

// Find the bit differences of existing all pairs in given array
int pairBitDifference(int arr[], int n)
{
// Define resultant variable
int result = 0;
// Define some counter variables
int i = 0;
int j = 0;
int counter = 0;
// Assume that array element is less than 32 bits
for (i = 0; i <= 31; ++i)
{
for (j = 0; j < n; ++j)
{
if (arr[j] & (1 << i))
{
// When position of (i) bits is active of element in array[j] number.
counter++;
}
}
// Calculate bit's difference
// (counter *n) Multiply number of active bits by number of  array element
// (counter *counter)  Multiply active bits of itself which is exist in (ith) position
result += ((counter *n) - (counter *counter)) *2;
// reset the bit counter
counter = 0;
}
return result;
}
int main(int argc, char const *argv[])
{
// Define array of integer elements
int arr[] = {
1 , 4 , 3
};
// Get the number of array elements
int n = sizeof(arr) / sizeof(arr);
// Get the difference of all bit pairs
int result = pairBitDifference(arr, n);
// Display calculated result
printf("\n Result : %d", result);
return 0;
}``````

#### Output

`` Result : 12``
``````/*
Java program
Sum of bit differences among all pairs
*/
public class BitDifference
{
// Find the bit differences of existing all pairs in given array
public int pairBitDifference(int[] arr, int n)
{
// Define resultant variable
int result = 0;
// Define some counter variables
int i = 0;
int j = 0;
int counter = 0;
// Assume that array element is less than 32 bits
for (i = 0; i <= 31; ++i)
{
for (j = 0; j < n; ++j)
{
if ((arr[j] & (1 << i)) == 0)
{
// When position of (i) bits is active of element in array[j] number.
counter++;
}
}
// Calculate bit's difference
// (counter * n) Multiply number of active bits by number of  array element
// (counter * counter)  Multiply active bits of itself which is exist in (ith) position
result += ((counter * n) - (counter * counter)) * 2;
// reset the bit counter
counter = 0;
}
return result;
}
public static void main(String[] args)
{
// Define array of integer elements
int[] arr = {
1 , 4 , 3
};
// Get the number of array elements
int n = arr.length;
// Get the difference of all bit pairs
// Display calculated result
System.out.print("\n Result : " + result);
}
}``````

#### Output

`` Result : 12``
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ program
Sum of bit differences among all pairs
*/

class BitDifference
{
public:
// Find the bit differences of existing all pairs in given array
int pairBitDifference(int arr[], int n)
{
// Define resultant variable
int result = 0;
// Define some counter variables
int i = 0;
int j = 0;
int counter = 0;
// Assume that array element is less than 32 bits
for (i = 0; i <= 31; ++i)
{
for (j = 0; j < n; ++j)
{
if ((arr[j] &(1 << i)) == 0)
{
// When position of (i) bits is active of element in array[j] number.
counter++;
}
}
// Calculate bit's difference
// (counter *n) Multiply number of active bits by number of  array element
// (counter *counter)  Multiply active bits of itself which is exist in (ith) position
result += ((counter *n) - (counter *counter)) *2;
// reset the bit counter
counter = 0;
}
return result;
}
};
int main()
{
// Define array of integer elements
int arr[] = {
1 , 4 , 3
};
// Get the number of array elements
int n = sizeof(arr) / sizeof(arr);
// Get the difference of all bit pairs
// Display calculated result
cout << "\n Result : " << result;
return 0;
}``````

#### Output

`` Result : 12``
``````// Include namespace system
using System;
/*
C# program
Sum of bit differences among all pairs
*/
public class BitDifference
{
// Find the bit differences of existing all pairs in given array
public int pairBitDifference(int[] arr, int n)
{
// Define resultant variable
int result = 0;
// Define some counter variables
int i = 0;
int j = 0;
int counter = 0;
// Assume that array element is less than 32 bits
for (i = 0; i <= 31; ++i)
{
for (j = 0; j < n; ++j)
{
if ((arr[j] & (1 << i)) == 0)
{
// When position of (i) bits is active of element in array[j] number.
counter++;
}
}
// Calculate bit's difference
// (counter * n) Multiply number of active bits by number of  array element
// (counter * counter)  Multiply active bits of itself which is exist in (ith) position
result += ((counter * n) - (counter * counter)) * 2;
// reset the bit counter
counter = 0;
}
return result;
}
public static void Main(String[] args)
{
// Define array of integer elements
int[] arr = {
1 , 4 , 3
};
// Get the number of array elements
int n = arr.Length;
// Get the difference of all bit pairs
// Display calculated result
Console.Write("\n Result : " + result);
}
}``````

#### Output

`` Result : 12``
``````<?php
/*
Php program
Sum of bit differences among all pairs
*/
class BitDifference
{
// Find the bit differences of existing all pairs in given array
public	function pairBitDifference( & \$arr, \$n)
{
// Define resultant variable
\$result = 0;
// Define some counter variables
\$i = 0;
\$j = 0;
\$counter = 0;
// Assume that array element is less than 32 bits
for (\$i = 0; \$i <= 31; ++\$i)
{
for (\$j = 0; \$j < \$n; ++\$j)
{
if ((\$arr[\$j] & (1 << \$i)) == 0)
{
// When position of (i) bits is active of element in array[j] number.
\$counter++;
}
}
// Calculate bit's difference
// (counter * n) Multiply number of active bits by number of  array element
// (counter * counter)  Multiply active bits of itself which is exist in (ith) position
\$result += ((\$counter * \$n) - (\$counter * \$counter)) * 2;
// reset the bit counter
\$counter = 0;
}
return \$result;
}
}

function main()
{
// Define array of integer elements
\$arr = array(1, 4, 3);
// Get the number of array elements
\$n = count(\$arr);
// Get the difference of all bit pairs
// Display calculated result
echo "\n Result : ". \$result;
}
main();``````

#### Output

`` Result : 12``
``````/*
Node Js program
Sum of bit differences among all pairs
*/
class BitDifference
{
// Find the bit differences of existing all pairs in given array
pairBitDifference(arr, n)
{
// Define resultant variable
var result = 0;
// Define some counter variables
var i = 0;
var j = 0;
var counter = 0;
// Assume that array element is less than 32 bits
for (i = 0; i <= 31; ++i)
{
for (j = 0; j < n; ++j)
{
if ((arr[j] & (1 << i)) == 0)
{
// When position of (i) bits is active of element in array[j] number.
counter++;
}
}
// Calculate bit's difference
// (counter * n) Multiply number of active bits by number of  array element
// (counter * counter)  Multiply active bits of itself which is exist in (ith) position
result += ((counter * n) - (counter * counter)) * 2;
// reset the bit counter
counter = 0;
}
return result;
}
}

function main()
{
// Define array of integer elements
var arr = [1, 4, 3];
// Get the number of array elements
var n = arr.length;
// Get the difference of all bit pairs
// Display calculated result
process.stdout.write("\n Result : " + result);
}
main();``````

#### Output

`` Result : 12``
``````#   Python 3 program
#   Sum of bit differences among all pairs

class BitDifference :
#  Find the bit differences of existing all pairs in given array
def pairBitDifference(self, arr, n) :
#  Define resultant variable
result = 0
#  Define some counter variables
i = 0
j = 0
counter = 0
#  Assume that array element is less than 32 bits
while (i <= 31) :
while (j < n) :
if ((arr[j] & (1 << i)) == 0) :
#  When position of (i) bits is active of element in array[j] number.
counter += 1

j += 1

#  Calculate bit's difference
#  (counter * n) Multiply number of active bits by number of  array element
#  (counter * counter)  Multiply active bits of itself which is exist in (ith) position
result += ((counter * n) - (counter * counter)) * 2
#  reset the bit counter
counter = 0
i += 1
j = 0

return result

def main() :
#  Define array of integer elements
arr = [1, 4, 3]
#  Get the number of array elements
n = len(arr)
#  Get the difference of all bit pairs
#  Display calculated result
print("\n Result : ", result, end = "")

if __name__ == "__main__": main()``````

#### Output

`` Result :  12``
``````#   Ruby program
#   Sum of bit differences among all pairs

class BitDifference
#  Find the bit differences of existing all pairs in given array
def pairBitDifference(arr, n)
#  Define resultant variable
result = 0
#  Define some counter variables
i = 0
j = 0
counter = 0
#  Assume that array element is less than 32 bits
while (i <= 31)
while (j < n)
if ((arr[j] & (1 << i)) == 0)
#  When position of (i) bits is active of element in array[j] number.
counter += 1
end

j += 1
end

#  Calculate bit's difference
#  (counter * n) Multiply number of active bits by number of  array element
#  (counter * counter)  Multiply active bits of itself which is exist in (ith) position
result += ((counter * n) - (counter * counter)) * 2
#  reset the bit counter
counter = 0
i += 1
j = 0
end

return result
end

end

def main()
#  Define array of integer elements
arr = [1, 4, 3]
#  Get the number of array elements
n = arr.length
#  Get the difference of all bit pairs
#  Display calculated result
print("\n Result : ", result)
end

main()``````

#### Output

`` Result : 12``
``````/*
Scala program
Sum of bit differences among all pairs
*/
class BitDifference
{
// Find the bit differences of existing all pairs in given array
def pairBitDifference(arr: Array[Int], n: Int): Int = {
// Define resultant variable
var result: Int = 0;
// Define some counter variables
var i: Int = 0;
var j: Int = 0;
var counter: Int = 0;
// Assume that array element is less than 32 bits
while (i <= 31)
{
while (j < n)
{
if ((arr(j) & (1 << i)) == 0)
{
// When position of (i) bits is active of element in array[j] number.
counter += 1;
}
j += 1;
}
// Calculate bit's difference
// (counter * n) Multiply number of active bits by number of  array element
// (counter * counter)  Multiply active bits of itself which is exist in (ith) position
result += ((counter * n) - (counter * counter)) * 2;
// reset the bit counter
counter = 0;
i += 1;
j = 0;
}
return result;
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: BitDifference = new BitDifference();
// Define array of integer elements
var arr: Array[Int] = Array(1, 4, 3);
// Get the number of array elements
var n: Int = arr.length;
// Get the difference of all bit pairs
var result: Int = task.pairBitDifference(arr, n);
// Display calculated result
print("\n Result : " + result);
}
}``````

#### Output

`` Result : 12``
``````/*
Swift 4 program
Sum of bit differences among all pairs
*/
class BitDifference
{
// Find the bit differences of existing all pairs in given array
func pairBitDifference(_ arr: [Int], _ n: Int)->Int
{
// Define resultant variable
var result: Int = 0;
// Define some counter variables
var i: Int = 0;
var j: Int = 0;
var counter: Int = 0;
// Assume that array element is less than 32 bits
while (i <= 31)
{
while (j < n)
{
if ((arr[j] & (1 << i)) == 0)
{
// When position of (i) bits is active of element in array[j] number.
counter += 1;
}
j += 1;
}
// Calculate bit"s difference
// (counter * n) Multiply number of active bits by number of  array element
// (counter * counter)  Multiply active bits of itself which is exist in (ith) position
result += ((counter * n) - (counter * counter)) * 2;
// reset the bit counter
counter = 0;
i += 1;
j = 0;
}
return result;
}
}
func main()
{
// Define array of integer elements
let arr: [Int] = [1, 4, 3];
// Get the number of array elements
let n: Int = arr.count;
// Get the difference of all bit pairs
let result: Int = task.pairBitDifference(arr, n);
// Display calculated result
print("\n Result : ", result, terminator: "");
}
main();``````

#### Output

`` Result :  12``
``````/*
Kotlin program
Sum of bit differences among all pairs
*/
class BitDifference
{
// Find the bit differences of existing all pairs in given array
fun pairBitDifference(arr: Array < Int > , n: Int): Int
{
// Define resultant variable
var result: Int = 0;
// Define some counter variables
var i: Int = 0;
var j: Int = 0;
var counter: Int = 0;
// Assume that array element is less than 32 bits
while (i <= 31)
{
while (j < n)
{
if ((arr[j] and(1 shl i)) == 0)
{
// When position of (i) bits is active of element in array[j] number.
counter += 1;
}
j += 1;
}
// Calculate bit's difference
// (counter * n) Multiply number of active bits by number of  array element
// (counter * counter)  Multiply active bits of itself which is exist in (ith) position
result += ((counter * n) - (counter * counter)) * 2;
// reset the bit counter
counter = 0;
i += 1;
j = 0;
}
return result;
}
}
fun main(args: Array <String> ): Unit
{
// Define array of integer elements
var arr: Array <Int> = arrayOf(1, 4, 3);
// Get the number of array elements
var n: Int = arr.count();
// Get the difference of all bit pairs
var result: Int = task.pairBitDifference(arr, n);
// Display calculated result
print("\n Result : " + result);
}``````

#### Output

`` Result : 12``

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