Hamming Distance Hamming distance

Source: Internet
Author: User
Intro

The first time I heard Hamming distance from an interview, then asked a topic:
An unsigned binary integer n,int length is known, and the number of 1 in binary is obtained.
Method 1: The simplest way to direct the number, one at a time, declare a count variable, when the mantissa is 1 plus 1, and then shift N to the right 1 bits, until the number is 0

int Method01 (int n)
{
	int count (0);	Declares a count variable
	while (n! = 0)
	{
		count + = n & 1;
		n >>= 1;	Move right
	}
	return count;
}
methods 2:n and n-1 and the lowest bit are always 0
Direct numbers This outrageous approach is certainly not inefficient, taking every one into account, without sifting.
Here is only the number of statistics 1, then how to take out the 11 one. Can not only count the number, but also can not affect the other statistics 1. Consider such a rule: N and n-1 phase, the lowest bit is always 0.

e.g.:

N 0101 1000
N-1 0101 0111
N & (N-1) 0101 0000

In this case, the last 1 has all been replaced by 0, while the other 1 has not had an impact.
int Method02 (int n)
{
	int count (0);
	while (n! = 0)
	{
		n &= n-1;
		++count;
	}
	return count;
}
This method is the most efficient of the majority of N's data bits 0 o'clock, because only one count is counted. Extension 1:If you determine the power of a number 2: (n > 0) && ((N & (n-1)) = = 0)
Extension 2:If the majority of n is 1 o'clock, you can replace n &= n-1 with n | (n-1) = 0xFF
Method 3:hamming Weight

Using the idea of splitting and metallurgy, in order to count the number of 4 bytes in 1, the adjacent two bits of data are divided into a group, the number of each group containing 1 is counted:
A group of two-bit x=10 than the adjacent, to count this group contains 1 of the number, the number of low 1: X & 01, the number of high in 1 (x >> 1) & 01. where n is 4byte, it is represented as N & 0x55555555 + (x >> 1) & 0x55555555.

The result of the previous step is saved to N, then each adjacent two groups of 4 bits constitute a new group, and then the calculation, so only 4 operations, the final result can be obtained.

e.g.: Take 8bit as an example

n = 0110 1100

0x55 = 0101 0101

--------------------------------------------------------------------------------------------------------------- ------

n = | 1|1 0|1 1|0 0| Divides n by two bits per set

--------------------------------------------------------------------------------------------------------------- -------

|-1|0 0|0 1|0 0| Results of N and 0x55=0101 0101 phase: 1 in low

+ | 0|0 1|0 1|0 0| N>>1 and 0x55=0101 0101 phase result: Number of 1 in high order

= | | 1|0 1|1 0|0 0| Add two Results

= | 1 0 1|1 0 0 0| 4 a group

--------------------------------------------------------------------------------------------------------------- -------

|-0 0 1|0 0 0 0| Results of N and 0x33=0011 0011 phases

+|0 0 0 1|0 0 1 0| Results of the n>>2 and 0x33=0011 0011 phases

=|0 0 1 0|0 0 1 0| Add

=|0 0 1 0 0 0 1 0| 8 A group

--------------------------------------------------------------------------------------------------------------- -------

| 0 0 0 0 0 1 0| Results of N and 0x0f=0x0000 1111 phases

+|0 0 0 0 0 0 1 0| Results of the n>>4 and 0x0f=0x0000 1111 phases

=|0 0 0 0 0 1 0 0| Add

= 4 The final result, which is 4


For the int type of 4byte, the code is as follows:

int method03_hammingweight (int n)
{
	n = (n & 0x55555555) + ((n >> 1) & 0x55555555);
	n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
	n = (n & 0x0f0f0f0f) + ((n >> 4) & 0x0f0f0f0f);
	n = (n & 0x00ff00ff) + ((n >> 8) & 0x00ff00ff);
	n = (n & 0x0000ffff) + ((n >> +) & 0x0000ffff);
	return n;
}


The code above is Hamming Weight


Code Code Link: https://github.com/chengfzy/BasicAlgorithm/tree/master/Q01_HammingWeight


application of similarity detection

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.