Refactor atBit and safeCast