Java Bit Count bitCount(byte b)

Here you can find the source of bitCount(byte b)

Description

Hamming distance of a byte with 0

License

Open Source License

Parameter

Parameter Description
b the byte on which we compute the hamming distance with 0

Return

the number of 1's in the binary representation of the byte

Declaration

public static short bitCount(byte b) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from   w w w  . j  a va2 s.c  o m
     * Hamming distance of a byte with 0
     * @param b the byte on which we compute the hamming distance with 0
     * @return the number of 1's in the binary representation of the byte
     */
    public static short bitCount(byte b) {
        short temp = (short) (b + 128);
        short count = 0;
        for (int i = 0; i < 8; i++) {
            count += (temp >> (7 - i)) % 2;
        }
        return count;
    }
}

Related

  1. bitCount(final Integer i)
  2. bitCount(int i)
  3. bitcount(int num)
  4. bitCount(int x)