Need some help with BURGER solution

I found this python code in the submissions. The way It uses some binary number properties to solve the problem seems amazing to me. But I could not understand the logic completely on my own. So I need some help. Is there anyone who can explain this code to me?
https://www.codechef.com/viewsolution/46830020