Hamming Distance - Two Integers

 Coding Problem Keys 

 Hamming Distance - Two Integers 

 Problem Statement 

The program must accept two integer values A and B. Then the program must print the Hamming Distance between them.
Note: The Hamming Distance between two integers is the number of positions at which the bits are different.

 Boundary Conditions 

1 <= A, B <= 10

 Input Format 

The first line contains A and B separated by a space.

 Output Format 

The first line contains an integer representing the Hamming Distance between A and B.

 Example Input/Output 1 

 Input 

12 4

 Output 

1

 Explanation 

The binary representation of 12 is 1100.
The binary representation of 4 is 0100.
The bits are different only in the first position.

 Example Input/Output 2 

 Input 

10 5

 Output 

4

 Note: Max Execution Time Limit: 50 millisecs 

 Solution 

 Programming Language: C Language 

#include<stdio.h>
#include<stdlib.h>
int main(){
    int a, b;
    scanf("%d %d", &a, &b);
    int res = a ^ b, count = 0;
    while(res > 0){
        if(res % 2 == 1) count ++; res /= 2;
    }printf("%d", count);
}
//Published By PKJCODERS

 Alter 

#include<stdio.h>
#include<stdlib.h>
int main(){
    int num1,num2;
    scanf("%d %d",&num1,&num2);
    int count=0;
    while(1){
        if(num1%2 != num2%2) count++;
        num1/=2;
        num2/=2;
        if(num1==0 && num2==0) break;
    }printf("%d",count);
}
//Published By PKJCODERS

 Programming Language: C++ Language 

#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv){
    int a,b,cnt=0;
    cin >> a >> b;
    while(a||b){
        if(a%2!=b%2)
           cnt++;
        a/=2,b/=2;
    }cout << cnt ;
}
//Published By PKJCODERS

 Alter 

#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv){
    int a[10001],b[10001],i,j,n1,n2,p,o=0,v=0,f=0;
    cin>>n1>>n2;
    while(n2){
        b[v++]=n2%2;
        n2/=2;
    }
    while(n1){
        a[o++]=n1%2;
        n1/=2;
    }
    if(v<o){
        p=v;
        for(i=p;i<o;i=i+1){
            b[v++]=0;
        }
    }
    if(o<v){
    p=o;
    for(i=p;i<v;i=i+1){
        a[o++]=0;
    }
}
    int c=0;
    for(i=v-1,j=o-1;i>=0,j>=0;i--,j--){
        if(b[i]!=a[j]){
            c++;
        }
    }cout<<c;
}
//Published By PKJCODERS

 Programming Language: Java Language 

import java.util.*;
public class Hello {
    public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int A=sc.nextInt(),B=sc.nextInt(),c=0;
		while(A>0 || B>0){
		    if(A%2!=B%2){
		        c++;
		    }
		    A/=2;B/=2;
		}
		System.out.print(c);
	}
}
//Published By PKJCODERS

 Alter 

import java.util.*;
public class Hello {
    public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int a=sc.nextInt();
		int b=sc.nextInt();
	    int s=0;
	    int x=a^b;
	    while(x>0){
	        s+=x&1;
	        x>>=1;
	    }System.out.print(s);
	}
}
//Published By PKJCODERS

 Programming Language: Python 3 Language 

a,b=map(int,input().split())
m=0
while a>0 or b>0:
    m+=(1 if a%2!=b%2 else 0)
    a//=2
    b//=2
print(m)
#Published By PKJCODERS

 Alter 

a,b=map(int,input().split())
z=[a,b]
z.sort()
w1=bin(z[0])[2:]
w2=bin(z[1])[2:]
w1=w1.rjust(len(w2),"0")
d=0
for x in range(len(w1)):
    if w1[x]!=w2[x]:
        d+=1
print(d)
#Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments