Java tutorial
//package com.java2s; import java.math.BigInteger; public class Main { public static int hammingWeight(byte[] bytes, int length) { int weight = 0; for (int i = 0; i <= length; i++) { if (bytes[i] != 0) weight++; } return weight; } public static int hammingWeight(BigInteger value) { int weight = 0; for (int i = 0; i <= value.bitLength(); i++) { if (value.testBit(i)) weight++; } return weight; } }