result
to 0 and MOD
to 1000000007 (10^9 + 7).length
to 0, which will keep track of the current length of the binary representation.i
from 1 to n
:
a. If i
is a power of 2, increment length
(since the binary representation of i
will be one bit longer).
b. Shift result
to the left by length
bits.
c. Use bitwise OR to add i
to result
.
d. Apply modulo MOD
to keep the result within the integer range.result
after the loop.