hi,

I recently wrote this code to calculate the hamming distance between any two binary numbers... The program accepts the numbers in decimals, performs a bitwise XOR operation and returns the HD...

However, it returns the Hamming distance in decimal... that is if after XOR it gets something like 0010... the hamming distance is 1 but the function returns 2 (the decimal conversion of 0010). how do i get it to return the number of 1s instead of the decimal conversion? The code i wrote is given below -

/*This program lets you enter any two decimal integer values... converts them to binary, performs an XOR operation on the binaries, and returns a decimal value for the binary obtained after XOR*/

``````#include<stdio.h>
#include<stdlib.h>
long int hammingdistance(long int,long int);
main()
{
long int dec1, dec2,x;
printf("\nEnter decimal 1\n");
scanf("%d",&dec1);
printf("Enter decimal 2\n");
scanf("%d",&dec2);
x = hammingdistance(dec1,dec2);
printf("hamming distance = %d", x);
}
/*this function calculates bitwise hamming distance for the given integers*/
long int hammingdistance(long int x, long int y)
{
long int dist, hdist;
hdist = x^y;

return (hdist);
}``````

## Recommended Answers

1. Hamming distance for binaries:
http://en.wikipedia.org/wiki/Hamming_distance
2. Use CODE tag for snippets: [code=c] your sources

[/code]

hi,

can anyone tell me what exactly this line of the code does?

``val &= val - 1;``

That's the same than: `val = val & val - 1;` Search for bitwise AND assignment, if you want to know more.

## All 5 Replies

Thanks...

Will keep the code tags in mind from next time on...

Cheers

hi,

can anyone tell me what exactly this line of the code does?

``val &= val - 1;``

this the code from which the line's been taken.... it calculates hamming distance...

``````unsigned hamdist(unsigned x, unsigned y)
{
unsigned dist = 0, val = x ^ y;

while(val)
{
++dist;
val &= val - 1;
}

return dist;
}``````

hi,

can anyone tell me what exactly this line of the code does?

``val &= val - 1;``

That's the same than: `val = val & val - 1;` Search for bitwise AND assignment, if you want to know more.

NICE WORK BY NISHant 3316......I WS DOING DIS IN BINARY WHICH WS MAKING IT VERY LONG CODE AND CONSUMINT LOTS OF TYM WHEN I INPUT NUM .YOUR CODE HELPED....THANKS
DIS IS WORKING:

``````// CALCULATE HAMMING DISTANCE BETWEEN ANY FIXED NO. OF BINARY NUMBERS(here 4) of any no. of bits in certain order

#include<stdio.h>
#include<stdlib.h>

void main()
{   long int d,i,j,x,n,dist,hdist;
clrscr();
// printf("enter the no. of numbers b/w which u want to find HDin order:\n");
// scanf("%d",&n);
for(i=0;i<4;i++)
{
printf("\ndecimal %d :",i+1);
scanf("%d",&d[i]);
}
for(i=0;i<3;i++)
{
for(j=i+1;j<=3;j++)
{
hdist = d[i]^d[j];
printf("%d\n",hdist);
}
}
getch();
}
/*************output**********

decimal 1 :7

decimal 2 :12

decimal 3 :3

decimal 4 :8
11
4
15
15
4
11                */
BUT THIS CREATING SOME PROBLEM WHY?????problem is inspite of putting limit(n) of entering d[i],continusly taking digits i.e crossing the n value or u cAn say tangled in loop.why??
#include<stdio.h>
#include<stdlib.h>

void main()
{   long int d,i,j,x,n,dist,hdist;
clrscr();
printf("enter the no. of numbers b/w which u want to find HDin order:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\ndecimal %d :",i+1);
scanf("%d",&d[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<=n-1;j++)
{
hdist = d[i]^d[j];
printf("%d\n",hdist);
}
}
getch();
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.