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);
}

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[4],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[10],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();
}

Edited 3 Years Ago by Dani: Formatting fixed

This article has been dead for over six months. Start a new discussion instead.