Compare two byte arrays and count their different bits

Before counting the number of different bits between the two byte data, the method I used is to first convert the two arrays into binary strings and then compare them, but if the two arrays contain a lot of data, use this This method is time-consuming, and it is not suitable for programs that require high processing speed. After testing, it is found that the speed of XOR comparison is much faster. The following is the source code of this method:
public int errorNum(byte[] data1,byte[] data2)
{
  int num=0;
  int c=0;
  for(int i=0;i<data1.length;i++)
{
  c=data1[i]^data2[i];
  while(c!=0)
 {
    c&=(c-1);
    sum++;
  }
}
 return sum;
}

Guess you like

Origin blog.csdn.net/youarenotme/article/details/80077742