Sum vs XOR

Sort by

recency

|

120 Discussions

|

  • + 0 comments

    Medium story to show a Python solution @nemat-al/sum-vs-xor-hackerrank-python-e3438578934d">https://medium.com/@nemat-al/sum-vs-xor-hackerrank-python-e3438578934d

  • + 0 comments
    def sumXor(n):
        return 1 << list(bin(n or 1)[2:][::-1]).count('0')
    
  • + 0 comments

    C#:

    public static long sumXor(long n)
    {
        long mask = n | n >> 1;
        mask |= mask >> 2;
        mask |= mask >> 4;
        mask |= mask >> 8;
        mask |= mask >> 16;
        mask |= mask >> 32;
        return 1L << (int)long.PopCount(mask ^ n);
    }
    
  • + 0 comments

    here is HackerRank Sum vs XOR problem solution in Python, java, C++ c and javascript

  • + 0 comments

    In JAVA8 :

    public static long sumXor(long n) {
            // Write your code here
            long count = 0;
            while (n > 0) {
                if ((n & 1) == 0) {
                    count++;
                }
                n >>= 1;
            }
            return 1L << count;
        }