Casting integers when bit-twiddling

This problem deserves a good general solution that's useful in other situations. See this post of mine, it's also about value range analysis and an integration of it with the into():