Sum vs XOR

  • + 0 comments

    java

        public static long sumXor(long n) {
            // Write your code here
            if(n == 0) return 1 ;
            
            int totalOneBits = Long.bitCount(n) ;
            int totalBits = Long.toBinaryString(n).length() ;
             int zeroBits = totalBits - totalOneBits ;
             
             return 1l << zeroBits ; //bitwise left shift => 8 4 2 1 ;power of 2
        }