I know we can set any bit in the byte by using logical OR and can clear any bit by logical AND like to
val |= (1<<order_number_of_bit_to_set); //for setting some specific number of bit
and for clearing a bit
val &= ~(1<<order_number_of_bit_to_clear); // specific bit to clear
but my question is how can we check that how many and which ordered number bits are set in the byte.
for example if we have
val = 0x22;
it means that 2nd and 5th bit is set in the byte
what is the efficient, quick and shortest way to do this?
The quick solution that came to mind is to iterate through all bits and check their order and if it is set record and display the order of the bit.
But is there any other efficient way to do this?