Optimize setSizeInBits

This PR contains an optimization in setSizeInBits and some code refactoring.