

RES = 0
FOR(i -> A to B) {
FOR(j -> C to D) {
ADD (i ^ j) to RES;
}
}
PRINT(RES)
The first line of the input contains a single integer 'T', representing the number of test cases.
The first line of each test case contains 4 space-separated integers, representing the integers ‘A’, ‘B’, ‘C’, and ‘D’.
For each test case, print the result of the code modulo 1000000007.
Print the output of each test case in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= A <= B <= 5*10^3
1 <= C <= D <= 5*10^3
Time Limit : 1 sec
The basic idea is to find the count of numbers of set bits and unset bits for every bit. A digit can contribute to the result if the bit is set. A bit can be set in 2 ways with the help of XOR. (1 ^ 0) = (0 ^ 1) = 1.
So we need to find the count of numbers from ‘A’ to ‘B’ which have their bit set and count of numbers from ‘C’ to ‘D’ which have their bit unset and vice versa to calculate the result.
Here is the algorithm :
The idea is to find the number of set bits from numbers 0 to ‘A’ - 1 and numbers from 0 to ‘B’ using shifting properties and subtract the result to get numbers having set bit at a particular bit from ‘A’ to ‘B’. We do the same process for numbers between ‘C’ to ‘D’. To calculate the result, we follow approach 1.
Here is the algorithm :
HELPER(N, I)