Example, say I want bits 16-23.
Code: Select all
uint32_t someBits;
uint8_t optionA = (someBits & 0xff0000) >> 16; //isolate the bits and shift
uint8_t optionB = (someBits << 8) >> 24; //double shift to clear unwanted bits
Code: Select all
uint32_t someBits;
uint8_t optionA = (someBits & 0xff0000) >> 16; //isolate the bits and shift
uint8_t optionB = (someBits << 8) >> 24; //double shift to clear unwanted bits
Code: Select all
uint8_t optionA( uint32_t someBits )
{
return (someBits & 0xff0000) >> 16;
}
uint8_t optionB( uint32_t someBits )
{
return (someBits << 8) >> 24;
}
uint8_t optionC( uint32_t someBits )
{
return someBits >> 16;
}
For this specific example; fastest would probably be:MuchLearning wrote:Example, say I want bits 16-23.Which is faster?Code: Select all
uint32_t someBits; uint8_t optionA = (someBits & 0xff0000) >> 16; //isolate the bits and shift uint8_t optionB = (someBits << 8) >> 24; //double shift to clear unwanted bits
Schol-R-LEA wrote:Perhaps we need to start over from the top:
@MuchLearning, why do you want to know? I am not saying you shouldn't want to know, but the intent is relevant here, because (as I said earlier) this is the sort of micro--optimization that is entirely irrelevant in actual practice - any gain or loss from it is going be swamped by other factors unless it is a bottleneck in a very tight loop.
Code: Select all
uint8_t optionC = (uint8_t)(someBits >> 16);