Troublesome Vijay Help

Problem: COWA19A Problem - CodeChef
This is my solution for Troublesome Vijay : CodeChef: Practical coding for everyone

Basically, I calculated how many numbers between 1 and N have their ith bit ‘1’ for all i (max 32). Then I added their contribution. I’ve checked it alongside the Brute-force method for N upto 1e6 and it works.
Maybe it’s something about overflowing at bigger values or corner cases that I’m unable to find.
Any help would be appreciated.