Java Utililty Methods Bit Count

List of utility methods to do Bit Count

Description

The list of methods to do Bit Count are organized into topic(s).

Method

shortbitCount(byte b)
Hamming distance of a byte with 0
short temp = (short) (b + 128);
short count = 0;
for (int i = 0; i < 8; i++) {
    count += (temp >> (7 - i)) % 2;
return count;
intbitCount(final Integer i)
bit Count
return Integer.bitCount(i);
intbitCount(int i)
bit Count
if (i == 0) {
    return 32;
int j = i;
int n = 0;
while (j != 0) {
    j &= (j - 1);
    ++n;
...
intbitcount(int num)
bitcount
int count = 0;
while (num > 0) {
    count += num % 2;
    num /= 2;
return count;
intbitCount(int x)
bit Count
x -= ((x >> 1) & 0x55555555);
x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
x = (((x >> 4) + x) & 0x0f0f0f0f);
x += (x >> 8);
x += (x >> 16);
return x & 0x0000003f;
intbitCount(String s)
bit Count
int total = 0;
for (int i = 0; i < s.length(); i++) {
    total += char2int(s.charAt(i));
return total;
intbitCountSlow(int x)
Count the number of set bits in an int;
int temp;
temp = 0x55555555;
x = (x & temp) + (x >>> 1 & temp);
temp = 0x33333333;
x = (x & temp) + (x >>> 2 & temp);
temp = 0x07070707;
x = (x & temp) + (x >>> 4 & temp);
temp = 0x000F000F;
...
intbitLength(byte[] bytes)
bit Length
return bitLength(bytes.length);
intbitLength(final int byteLength)
Returns the bit length of the specified byte length.
return byteLength * 8;
intbitLength(int num)
bit Length
return (int) Math.ceil(Math.log((double) num) / Math.log(2));