Total Hamming Distance In C++

In this article, we will learn about the total hamming distance in C++ with several examples. The dissimilarity between two strings of equal length, usually binary strings, is measured using a matrix called total hamming distance. It measures the difference in bits between two strings' corresponding positions in terms of computer science and information theory. The total distance between any two elements in a string is the sum of the individual Hamming distances for each pair. We can iteratively calculate the total Hamming distance in C++ by counting the number of zeros and ones at each bit position across all strings. After that, the product of the counts of 1's and 0's at that location yields the total Hamming distance. In the end, we add up these products for every bit position.

Input arr = 7, 13, 3

First, we'll convert these decimal numbers to binary to find the Hamming distance:

7 in binary: 0111

13 in binary: 1101

3 in binary: 0011

Now, let's calculate the Hamming distance between each pair and sum them up:

hamming(7, 13) = 3

hamming(13, 3) = 3

hamming(7, 3) = 2

Adding them together: 3 + 3 + 2 = 8

So, the total Hamming distance is 8.

Example 1:

Let us take an example to illustrate the Total Hamming Distance in C++.

Example

#include <iostream>
#include <vector>

int totalHammingDistance(const std::vector<int>& nums) {
    int totalDist = 0;
    int n = nums.size();

    for (int i = 0; i < 32; ++i) { // Assuming integers are 32-bit
        int bitCount = 0;
        for (int num : nums) {
            bitCount += (num >> i) & 1; // Counting set bits at i-th position
        }
        totalDist += bitCount * (n - bitCount); // Hamming distance at i-th position
    }

    return totalDist;
}

int main() {
    std::vector<int> nums = {4, 14, 2};
    int totalDistance = totalHammingDistance(nums);
    std::cout << "Total Hamming Distance: " << totalDistance << std::endl;
    return 0;
}

Output:

Output

Total Hamming Distance: 6

Explanation:

  • In this example, the totalHammingDistance function is still present in the program. It calculates the total Hamming distance for an integer vector.
  • Iterating through each bit position (assuming 32-bit integers in this example) is similar to the previous example.
  • Next, the number of set bits ( 0's ) for each bit position is counted.
  • After that, the count of set bits and the count of unset bits ( 0's ) at that position are multiplied to get the total Hamming distance, which is then added.
  • Ultimately, it yields the overall Hamming distance.
  • Example 2:

Let us take another example to illustrate the Total Hamming Distance in C++.

Example

#include <bits/stdc++.h>
using namespace std;

// Function to calculate Hamming distance
int calculateHammingDistance(int num1, int num2) {
    int distance = 0;
    int maxValue = max(num1, num2);
    
    while (maxValue) {
        int bit1 = num1 & 1;
        int bit2 = num2 & 1;
        
        if (bit1 != bit2)
            distance++;
        
        maxValue >>= 1;
        num1 >>= 1;
        num2 >>= 1;
    }
    
    return distance;
}

// Main function
int main() {
    int number1 = 6, number2 = 10; // Change the numbers here
    int hammingDistance = calculateHammingDistance(number1, number2);
    
    // Printing the result
    cout << "Hamming Distance between " << number1 << " and " << number2 << " is: " << hammingDistance << endl;

    return 0;
}

Output:

Output

Hamming Distance between 6 and 10 is: 2

Example 3:

Let us take another example to illustrate the Total Hamming Distance in C++.

Example

#include <iostream>

// Function to calculate Hamming distance
int hammingDistance(int x, int y) {
    int distance = 0;
    
    // XOR operation to find differing bits
    int xorResult = x ^ y;
    
    // Counting set bits in the XOR result
    while (xorResult) {
        distance += xorResult & 1;
        xorResult >>= 1;
    }
    
    return distance;
}

// Main function
int main() {
    int n1 = 4, n2 = 8; // Change the numbers here
    int hdist = hammingDistance(n1, n2);
    std::cout << "Hamming Distance between " << n1 << " and " << n2 << " is: " << hdist << std::endl;
    return 0;
}

Output:

Output

Hamming Distance between 4 and 8 is: 2

Conclusion:

In conclusion, calculating the total Hamming distance in C++ is a useful tool for understanding how different binary strings are from one another. This information can be applied to a variety of fields, including coding theory, error detection, and DNA sequence analysis. C++ provides a variety of ways to compute the total Hamming distance through effective bit manipulation techniques. C++ algorithms precisely measure the total dissimilarity between sets of binary strings by iterating through corresponding bits of the input integers and counting differing bits. These implementations show how well the language handles bitwise operations, guaranteeing computational efficiency even with big datasets. Overall, C++ gives programmers strong tools for evaluating and measuring the Hamming distance, which helps in a variety of scenarios involving the solution of problems in various fields.

Input Required

This code uses input(). Please provide values below: