One variation of this problem is


  • 0
    Y

    find bitwise XOR of numbers in range:

    int rangeBitwiseXor(int m, int n){
        int res = 0;
        for(int i=0; i<=30; i++){
            unsigned int n_1 = (unsigned int)n/(unsigned int)(1<<(i+1));
            n_1 *= (unsigned int)(1<<i);
            unsigned int x = (unsigned int)n/(unsigned int)(1<<(i+1))*(unsigned int)(1<<(i+1));
            x = x==0?-1:x-1;
            int temp = ((unsigned int)n-x)-(1<<i);
            temp = temp<0?0:temp;
            n_1 += temp;
    
            unsigned int m_1 = (unsigned int)m/(unsigned int)(1<<(i+1));
            m_1 *= (unsigned int)(1<<i);
            x = (unsigned int)m/(unsigned int)(1<<(i+1))*(unsigned int)(1<<(i+1));
            x = x==0?-1:x-1;
            temp = ((unsigned int)m-x)-(1<<i)-1;
            temp = temp<0?0:temp;
            m_1 += temp;
    
            res |= (n_1-m_1)%2==0?0:(1<<i);
        }
        return res;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.