• + 0 comments

    More at https://github.com/pakbungdesu/hackerrank-problem-solving.git

    Java

    public static long sumXor(long n) {
        if (n == 0) {
            return 1;
        } else {
            String bin = Long.toBinaryString(n);
            int zero_bits = bin.length() - bin.replace("0", "").length();
            return (long)Math.pow(2, zero_bits);
        }
    }
    

    Pyhon

    def sumXor(n):
        if n == 0:
            return 1
        else:
            zero_bits = bin(n)[2:].count('0')  # Ignore the '0b' prefix
            return 2**zero_bits