The C compiler can translate this to recommended (i / 2U) * 2U
solution for unsigned numbers because of no need for 0xFF..FEu
logical AND constant operand which can't be directly encoded as assembly instruction immediate at some cores (e.g. some Cortex ARMs) or the -Os
compiler option favour this solution.
In assembly language, this mathematical expression turns to only two instructions - logical shift right and logical shift left without carry.
Alternative mathematical explanation not using the binary representation shifted right and left is, that the (i / 2U) * 2U
results in nearest lower even number (result is the i
again or i - 1U
if i
is odd). This means that the odd element was only removed from the number which is the 1*2^0
-> the lsb in binary representation.