hamming Weight BigInteger - Java java.math

Java examples for java.math:BigInteger

Description

hamming Weight BigInteger

Demo Code


import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Arraycopy;
import java.util.Random;
import static java.math.BigInteger.ONE;
import static java.math.BigInteger.ZERO;

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++;/*from  www .j  a  va 2  s .  c om*/
        }
        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;
    }
}

Related Tutorials