Nice. You need to initialize x to 0 instead of -m&m though, or you miss the solution with x=0.
In any case, it's a nice succinct trick for iterating through the values compatible with a mask. Getting the boundary conditions right is less important than knowing the trick.