Can the copy_bit
function below be simplified to something like out[out_bit] = in[in_bit]
? (i.e. Not using an if
statement)
template< typename T >
inline void copy_bit( T& out, const T in, const std::size_t out_bit, const std::size_t in_bit )
{
if ( (in & (1 << in_bit)) != 0 )
{
out |= (1 << out_bit); // Set bit
}
else
{
out &= ~(1 << out_bit); // Clear bit
}
}
// Set bit 4 in x to bit 11 in y
copy_bit( x, y, 4, 11 );
Update: Just to be clear, this isn't homework or an XY problem where suggesting std::bitset
answers the question.