Skip to content

Instantly share code, notes, and snippets.

@Cartman0
Last active August 29, 2015 14:20
Show Gist options
  • Save Cartman0/26cbf56d8277225d9cfe to your computer and use it in GitHub Desktop.
Save Cartman0/26cbf56d8277225d9cfe to your computer and use it in GitHub Desktop.
Java の整数から2進数のときの1bitをカウントする関数(int32bitのとき)
/*
解説
http://www.mwsoft.jp/programming/java/java_lang_integer_bit_count.html
*/
public static int bitCount(int i) {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment