Skip to content

Instantly share code, notes, and snippets.

@Plutor
Created March 8, 2012 18:15
Show Gist options
  • Save Plutor/2002457 to your computer and use it in GitHub Desktop.
Save Plutor/2002457 to your computer and use it in GitHub Desktop.
Integer.bitCount()
// This is the source code for Integer.bitCount() for Java 1.5+
// <http://www.docjar.com/html/api/java/lang/Integer.java.html> line 1132
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