Java Utililty Methods Bit Count

List of utility methods to do Bit Count

  1. HOME
  2. Java
  3. B
  4. Bit Count

Description

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

Method

short bitCount(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;
int bitCount(final Integer i)
bit Count
return Integer.bitCount(i);
int bitCount(int i)
bit Count
if (i == 0) {
 return 32;
int j = i;
int n = 0;
while (j != 0) {
 j &= (j - 1);
 ++n;
...
int bitcount(int num)
bitcount
int count = 0;
while (num > 0) {
 count += num % 2;
 num /= 2;
return count;
int bitCount(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;
int bitCount(String s)
bit Count
int total = 0;
for (int i = 0; i < s.length(); i++) {
 total += char2int(s.charAt(i));
return total;
int bitCountSlow(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;
...
int bitLength(byte[] bytes)
bit Length
return bitLength(bytes.length);
int bitLength(final int byteLength)
Returns the bit length of the specified byte length.
return byteLength * 8;
int bitLength(int num)
bit Length
return (int) Math.ceil(Math.log((double) num) / Math.log(2));


AltStyle によって変換されたページ (->オリジナル) /