5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

32bit整数のビットのハミング距離を求める

Posted at

符号化したデータがどれくらい違うかを求めるのに使える。

public static int getHammingDistance(int x1, int x2) {
	// 2つの整数のXORを取った結果が違いのあるビット
	int i = x1 ^ x2;

	// ビット数を計算
	// JDKのInteger#bitCount()より
	i -= (i >> 1) & 0x55555555;
	i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
	i = ((i >> 4) + i) & 0x0F0F0F0F;
	i += i >> 8;
	i += i >> 16;

	return i & 0x0000003F;
}
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?