Troublesome Vijay Help

This is my solution for Troublesome Vijay :

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.