How to calculate Hamming distance (Hamming distance)

Source: Internet
Author: User
Tags diff

int diff = 0;

for (int i = 0; i < DIM/64; i++)

{

Unsigned__int64 x1 = ((Unsigned__int64 *) px1) [i];

Unsigned__int64 x2 = ((Unsigned__int64 *) px2) [i];

Unsigned__int64 x = x1 ^ x2;

diff + = (int) _mm_popcnt_u64 (x);

}

int diff = 0;

for (int i = 0; i < len; i++)

{

ULONG T = a[i] ^ b[i];

T-= ((t >> 1) & 0x5555555555555555ul);

t = (T & 0x3333333333333333ul) + ((T >> 2) & 0x3333333333333333ul);

T = ((t + (t >> 4) & 0x0f0f0f0f0f0f0f0ful) * 0x0101010101010101ul) >> 56;

diff + = (int) T;

}

#if!defined have_bitcount_h__ #define HAVE_BITCOUNT_H__//This file was part of the FXT library. Copyright (C), Joerg Arndt//LICENSE:GNU general public License version 3 or later,//See the file C

OPYING.txt in the main directory. #include "fxttypes.h" #include "bits/bitsperlong.h" #include "bits/bitasm.h" static inline ulong Bit_count (ULONG x)//R
Eturn number of set bits.  The sequence of values returned for x = 0, 1, 2, 3, ... is//0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, .../
(Oeis sequence A000120).

{#ifdef HAVE_AMD64_POPCNT//Currently not auto-detected return Asm_bit_count (x); #else//have_amd64_popcnt #if Bits_per_long = = + +////VERSION 1://x = (0x55555555ul & x) + (0x55555555u  L & (x>> 1));  0-2 in 2 bits//x = (0x33333333ul & x) + (0x33333333ul & (x>> 2));  0-4 in 4 bits//x = (0x0f0f0f0ful & x) + (0x0f0f0f0ful & (x>> 4)); 0-8 in 8 bits//x = (0x00ff00fful & X) + (0x00ff00fful & (x>> 8));  0-16 in + bits//x = (0x0000fffful & x) + (0x0000fffful & (x>>16));

0-32 in + bits//return x;  VERSION 2://x = ((x>>1) & 0x55555555ul) + (x & 0x55555555ul);  0-2 in 2 bits//x = ((x>>2) & 0x33333333ul) + (x & 0x33333333ul);                   0-4 in 4 bits//x = ((x>>4) + x) & 0x0f0f0f0ful;                                        0-8 in 8 bits//x + = X>> 8;                                        0-16 in 8 bits//x + = x>>16;

    0-32 in 8 bits//return x & 0xFF;                        VERSION 3:x-= (x>>1) & 0x55555555ul;  0-2 in 2 bits x = ((x>>2) & 0x33333333ul) + (x & 0x33333333ul);                   0-4 in 4 bits x = ((x>>4) + x) & 0x0f0f0f0ful;
    0-8 in 8 bits x *= 0x01010101ul;

Return x>>24; #endif #if BITS_per_long = = +////VERSION 1://x = (0x5555555555555555ul & x) + (0x5555555555555555ul & (x>> 1))  ;  0-2 in 2 bits//x = (0x3333333333333333ul & x) + (0x3333333333333333ul & (x>> 2));  0-4 in 4 bits//x = (0x0f0f0f0f0f0f0f0ful & x) + (0x0f0f0f0f0f0f0f0ful & (x>> 4));  0-8 in 8 bits//x = (0x00ff00ff00ff00fful & x) + (0x00ff00ff00ff00fful & (x>> 8));  0-16 in + bits//x = (0x0000ffff0000fffful & x) + (0x0000ffff0000fffful & (x>>16));  0-32 in + bits//x = (0x00000000fffffffful & x) + (0x00000000fffffffful & (x>>32));

0-64 in bits//return x;  VERSION 2://x = ((x>>1) & 0x5555555555555555ul) + (x & 0x5555555555555555ul);  0-2 in 2 bits//x = ((x>>2) & 0x3333333333333333ul) + (x & 0x3333333333333333ul);                          0-4 in 4 bits//x = ((x>>4) + x) & 0x0f0f0f0f0f0f0f0ful; 0-8 in 8 bits//x + = X>> 8;                                                       0-16 in 8 bits//x + = x>>16;                                                       0-32 in 8 bits//x + = x>>32;

    0-64 in 8 bits//return x & 0xFF;                                VERSION 3:x-= (x>>1) & 0x5555555555555555ul;  0-2 in 2 bits x = ((x>>2) & 0x3333333333333333ul) + (x & 0x3333333333333333ul);                           0-4 in 4 bits x = ((x>>4) + x) & 0x0f0f0f0f0f0f0f0ful;
    0-8 in 8 bits x *= 0x0101010101010101ul;

Return x>>56; #endif #endif//have_amd64_popcnt}//-------------------------static inline ulong bit_count_15 (ULONG x)//Return Nu
Mber of set bits, must has at most set bits.                        {#if Bits_per_long = = x-= (x>>1) & 0x55555555ul; 0-2 in 2 bits x  = ((x>>2) & 0x33333333ul) + (x & 0x33333333ul);
    0-4 in 4 bits x *= 0x11111111ul;
Return x>>28;                                #endif #if Bits_per_long = = x-= (x>>1) & 0x5555555555555555ul;  0-2 in 2 bits x = ((x>>2) & 0x3333333333333333ul) + (x & 0x3333333333333333ul);
    0-4 in 4 bits x *= 0x1111111111111111ul;
Return x>>60;  #endif}//-------------------------static inline ulong Bit_count_3 (ULONG x)//Return number of set bits, must has at
Most 3 set bits.  {#if Bits_per_long = = x-= (x>>1) & 0x55555555ul;
    0-2 in 2 bits x *= 0x55555555ul;
Return x>>30;  #endif #if Bits_per_long = = x-= (x>>1) & 0x5555555555555555ul;
    0-2 in 2 bits x *= 0x5555555555555555ul;
Return x>>62;  #endif}//-------------------------static inline int bit_count_cmp (const ULONG &a, const ULONG &AMP;B)//Compare Bit counts of a and B.
{ULONG CA = Bit_count (a);
    ULONG cb = Bit_count (b);
Return (CA==CB 0: (CA&GT;CB +1:-1));
}//-------------------------static inline ulong Bit_count_sparse (ULONG x)//Return number of bits set.
    {#if 0//The loop would execute once for each bit of x set:ulong n = 0;  while (x) {++n; x &= (X-1);

} return N;
    #else//The loop would execute Ceil (C/4) if C bits of x are set:ulong n = 0;  do {n + = (x!=0);
        x &= (X-1);  n + = (x!=0);
        x &= (X-1);  n + = (x!=0);
        x &= (X-1);  n + = (x!=0);
    x &= (X-1);
    } while (x);

return n;
#endif}//-------------------------static inline ulong Bit_count_dense (ULONG x)//Return number of bits set. The Loop (of Bit_count_sparse ()) would execute once for//each unset bit (i.e. zero) of X. {return Bits_per_lon
G-bit_count_sparse (~X); }//-------------------------static inline ulong Bit_block_count (ULONG x)//RetUrn number of bit blocks. e.g.://.. 1..11111...111.3//... 1..11111...111-3//... 1.....1.1.3//.....
    111.1111-2 {//Return Bit_count (Gray_code (x))/2 + (x & 1);
Return (x & 1) + Bit_count ((x^ (x>>1)))/2; }//-------------------------static inline ulong Bit_block_ge2_count (ULONG x)//Return number of bit blocks with at l
East 2 bits. e.g.://.. 1..11111...111.2//... 1..11111...111-2//... 1.....1.1.0//.....
    111.1111-2 {//Return Bit_block_count (Interior_ones (x));
return Bit_block_count (x & (X<<1) & (x>>1));  }//-------------------------static inline ulong bit_count_01 (ULONG x)//Return number of bits in a word//for words
    of the special form 00...0001...11 {#if defined bits_use_asm if (1>=x) return x;
    x = ASM_BSR (x);
return x + 1;
    #else//bits_use_asm ulong ct = 0;

ULONG A; #if Bits_per_long = = 64 A = (X & (1UL&LT;&LT;32)) >> (32-5);  Test bit x >>= A;
CT + = A;  #endif a = (X & (1UL&LT;&LT;16)) >> (16-4);  Test bit x >>= A;

    CT + = A;  A = (x & (1ul<<8)) >> (8-3);  Test bit 8 x >>= A;

    CT + = A;  A = (x & (1ul<<4)) >> (4-2);  Test bit 4 x >>= A;

    CT + = A;  A = (x & (1UL&LT;&LT;2)) >> (2-1);  Test bit 2 x >>= A;

    CT + = A;  A = (x & (1ul<<1)) >> (1-0);  Test bit 1 x >>= A;

    CT + = A;  CT + + x & 1;

Test bit 0 return CT; 


#endif//Bits_use_asm}//-------------------------//Bits/bitcount-v.cc:ulong bit_count_v (const ULONG *x, ulong N);
 #endif//!defined have_bitcount_h__

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.