Sum vs XOR

  • + 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);
    }