Here you can find the source of countBits(byte num)
public static int countBits(byte num)
//package com.java2s; //License from project: Apache License public class Main { public static int countBits(byte num) { int count = 0; for (int i = 0; i < 8; i++) { if ((num & 1) == 1) // check if right most bit is 1 {//from ww w . j a v a 2 s.co m count++; } num = (byte) (num >>> 1); // shit right 1 bit, including the sign // bit } return count; } }