I have a table with different codes. And their Id’s are powers of 2. (20, 21, 22, 23…).
Based on different conditions my application will assign a value to the “Status” variable.
for ex :
Status = 272 ( which is 28+ 24)
Status = 21 ( Which is 24+ 22+20)
If Status = 21 then my method (C#) should tell me that 21 is sum of 16 + 4 + 1.
for
QStatus == 21it will giveExplanation:
A power of 2 has exactly one
1in its binary representation. We take that one to be the rightmost one(least significant) and iteratively push it leftwards(towards more significant) until the number overflows and becomes0. Each time we check thatcurrentPow & QStatusis not 0.