• + 0 comments

    Python3 solution

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    def count1s(n,i):
        if i == 1:
            return n & 1
        mask = 1 << i - 1
        remainder = (0xFFFFFFFF >> (32-i+1))&n
        return (n & mask > 0) * ((mask * (i - 1) >> 1) + 1 + remainder) + count1s(remainder, i - 1)
    
    def startCount():
        iterations = int(input())
        lis = []
        while iterations > 0:
            n1, n2 = [int(x) for x in input().split()] 
            lis.append([n1, n2])
            iterations -= 1
        for element in lis:
            if (element[0] > 0) or (element[1] < 0):
                print(count1s(element[1], 32) - count1s(element[0] - 1, 32))
            else:
                print((1 << 36) - count1s(element[0] - 1, 32) + count1s(element[1], 32))
    
    startCount()