ive dynamically allocated an array of two bytes using:
char *ptr = (char *)malloc (2*sizeof(char));
how can I find the Hamming distance between the two bytes?
ive dynamically allocated an array of two bytes using:
char *ptr = (char *)malloc (2*sizeof(char));
how can I find the Hamming distance between the two bytes?
Do you mean this?
I don't see how the allocation of two bytes has anything to do with it...
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
the example section of that page says:
The Hamming distance between:
10 *1* 1 *1* 01 and 10 *0* 1 *0* 01 is 2.
im trying to find the Hamming Distance between the two bytes in the memory allocated.
I'd say you should xor them together, and count the number of 1's from the return of the xoring. To count the number of 1s one solution might be something like... Change this to work for your own situation (note I haven't tested this)
Or see the wiki link code. Prolly better and more thought out.Code:int c, xor_res = thinga^thingb; int hamm_dist; for( c=1; c; c<<=1 ) hamm_dist += (xor_res & c ); printf( "Hamming distance between %d and %d, is %d\n", thinga, thingb, hamm_dist );