Distancia de Hamming entre dos enteros

Dados dos enteros, la tarea es encontrar la distancia de Hamming entre dos enteros. La distancia de Hamming entre dos enteros es el número de bits que son diferentes en la misma posición en ambos números. 
Ejemplos: 
 

Input: n1 = 9, n2 = 14
Output: 3
9 = 1001, 14 = 1110
No. of Different bits = 3

Input: n1 = 4, n2 = 8
Output: 2

Acercarse: 
 

  1. Calcular el XOR de dos números.
  2. Cuente el número de bits establecidos .

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate hamming distance
int hammingDistance(int n1, int n2)
{
    int x = n1 ^ n2;
    int setBits = 0;
 
    while (x > 0) {
        setBits += x & 1;
        x >>= 1;
    }
 
    return setBits;
}
 
// Driver code
int main()
{
    int n1 = 9, n2 = 14;
    cout << hammingDistance(9, 14) << endl;
 
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta (kriSania804)

C

// C implementation of above approach
#include <stdio.h>
 
// Function to calculate hamming distance
int hammingDistance(int n1, int n2)
{
    int x = n1 ^ n2;
    int setBits = 0;
 
    while (x > 0) {
        setBits += x & 1;
        x >>= 1;
    }
 
    return setBits;
}
 
// Driver code
int main()
{
    int n1 = 9, n2 = 14;
    printf("%d\n", hammingDistance(9, 14));
 
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta (kriSania804)

Java

// Java implementation of above approach
class GFG
{
 
// Function to calculate hamming distance
static int hammingDistance(int n1, int n2)
{
    int x = n1 ^ n2;
    int setBits = 0;
 
    while (x > 0)
    {
        setBits += x & 1;
        x >>= 1;
    }
 
    return setBits;
}
 
// Driver code
public static void main(String[] args)
{
    int n1 = 9, n2 = 14;
    System.out.println(hammingDistance(n1, n2));
}
}
 
// This code is contributed by Bilal

Python3

# Python3 implementation of above approach
 
# Function to calculate hamming distance
def hammingDistance(n1, n2) :
 
    x = n1 ^ n2
    setBits = 0
 
    while (x > 0) :
        setBits += x & 1
        x >>= 1
     
    return setBits
 
if __name__=='__main__':
    n1 = 9
    n2 = 14
    print(hammingDistance(9, 14))
 
# this code is contributed by Smitha Dinesh Semwal

C#

// C# implementation of above approach
class GFG
{
 
// Function to calculate
// hamming distance
static int hammingDistance(int n1, int n2)
{
    int x = n1 ^ n2;
    int setBits = 0;
 
    while (x > 0)
    {
        setBits += x & 1;
        x >>= 1;
    }
 
    return setBits;
}
 
// Driver code
static void Main()
{
    int n1 = 9, n2 = 14;
    System.Console.WriteLine(hammingDistance(n1, n2));
}
}
 
// This code is contributed by mits

PHP

<?PHP
// PHP implementation of above approach
 
// Function to calculate hamming distance
function hammingDistance($n1, $n2)
{
    $x = $n1 ^ $n2;
    $setBits = 0;
 
    while ($x > 0)
    {
        $setBits += $x & 1;
        $x >>= 1;
    }
 
    return $setBits;
}
 
// Driver code
$n1 = 9;
$n2 = 14;
echo(hammingDistance(9, 14));
 
// This code is contributed by Smitha
?>

Javascript

<script>
 
// Javascript implementation of above approach
 
// Function to calculate hamming distance
function hammingDistance(n1, n2)
{
    let x = n1 ^ n2;
    let setBits = 0;
 
    while (x > 0) {
        setBits += x & 1;
        x >>= 1;
    }
 
    return setBits;
}
 
// Driver code
    let n1 = 9, n2 = 14;
    document.write(hammingDistance(9, 14));
 
</script>
Producción

3

Nota: El número de bits establecidos se puede contar usando la función __builtin_popcount().

Enfoque 2:

1. Calcular el máximo de ambos números.

2.Compruebe los bits establecidos para ambos en cada posición.

C++

#include <bits/stdc++.h>
using namespace std;
int hammingDistance(int x, int y)
{
    int ans = 0;
    int m = max(x, y);
    while (m) {
        int c1 = x & 1;
        int c2 = y & 1;
        if (c1 != c2)
            ans += 1;
        m = m >> 1;
        x = x >> 1;
        y = y >> 1;
    }
    return ans;
}
int main()
{
    int n1 = 4, n2 = 8;
    int hdist = hammingDistance(n1, n2);
    cout << hdist << endl;
    return 0;
}

Java

/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    static int hammingDistance(int x, int y)
{
    int ans = 0;
    int m = Math.max(x, y);
    while (m>0) {
        int c1 = x & 1;
        int c2 = y & 1;
        if (c1 != c2)
            ans += 1;
        m = m >> 1;
        x = x >> 1;
        y = y >> 1;
    }
    return ans;
}
     
// Drivers code
public static void main(String args[])
{
    int n1 = 4, n2 = 8;
    int hdist = hammingDistance(n1, n2);
    System.out.println(hdist);
}
}
 
// This code is contributed by shinjanpatra

Python3

# Python3 code for the same approach
def hammingDistance(x, y):
 
   ans = 0
   m = max(x, y)
   while (m):
      c1 = x & 1
      c2 = y & 1
      if (c1 != c2):
         ans += 1
      m = m >> 1
      x = x >> 1
      y = y >> 1
   return ans
 
# driver program
n1 = 4
n2 = 8
hdist = hammingDistance(n1, n2)
print(hdist)
 
# This code is contributed by shinjanpatra

C#

// C# code to implement the approach
 
using System;
 
public class GFG
{
   
    // Function that return the hamming distance
    // between x and y
    static int hammingDistance(int x, int y)
    {
        int ans = 0;
        int m = Math.Max(x, y);
       
        // checking the set bits
        while (m > 0) {
            int c1 = x & 1;
            int c2 = y & 1;
            if (c1 != c2)
                ans += 1;
            m = m >> 1;
            x = x >> 1;
            y = y >> 1;
        }
        return ans;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int n1 = 4, n2 = 8;
        // Function call
        int hdist = hammingDistance(n1, n2);
        Console.WriteLine(hdist);
    }
}
 
// This code is contributed by phasing17

Javascript

<script>
 
// JavaScript code for the same approach
 
function hammingDistance(x,y)
{
    let ans = 0;
    let m = Math.max(x, y);
    while (m) {
        let c1 = x & 1;
        let c2 = y & 1;
        if (c1 != c2)
            ans += 1;
        m = m >> 1;
        x = x >> 1;
        y = y >> 1;
    }
    return ans;
}
 
// driver program
let n1 = 4,n2 = 8;
let hdist = hammingDistance(n1, n2);
document.write(hdist,"</br>");
 
// This code is contributed by shinjanpatra
 
</script>
Producción

2

Publicación traducida automáticamente

Artículo escrito por Smitha Dinesh Semwal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *