Thread: Hamming Distance?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    20

    Hamming Distance?

    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?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Do you mean this?

    I don't see how the allocation of two bytes has anything to do with it...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    20
    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.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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)

    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 &#37;d and %d, is %d\n", thinga, thingb, hamm_dist );
    Or see the wiki link code. Prolly better and more thought out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Line-Line distance
    By Waterbottle in forum C++ Programming
    Replies: 10
    Last Post: 04-24-2008, 03:03 PM
  2. Segfaulting Distance Program
    By radiohead in forum C Programming
    Replies: 2
    Last Post: 01-09-2006, 08:48 PM
  3. Distance Formula in my program..... I need help fast!!!
    By Mackology101 in forum C Programming
    Replies: 3
    Last Post: 09-23-2004, 10:10 PM
  4. Distance Formula Implecations: Urgent!
    By KneeLess in forum C Programming
    Replies: 6
    Last Post: 03-20-2004, 10:52 PM
  5. Fuzzy Logic
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-13-2002, 04:58 PM