0

If I have

FLAG_VAL = 1 << 6; //(or 64),

how can I get back the exponent (6 in this case) later in the code using FLAG_VAL and bit shifts?

Thank you.

theduck
  • 2,589
  • 13
  • 17
  • 23
  • 3
    Count the number of low-order zero bits. – Barmar Jan 05 '20 at 18:13
  • @Barmar this is valid only if there're no other bit set. The result is not correct if FLAG_VAL was 7. Obviously if FLAG_VAL is obtained always whit shifting 1 your suggestion is correct. – Sir Jo Black Jan 05 '20 at 18:21
  • 1
    @SirJoBlack It's not merging the old value of `FLAG_VAL`, it's just assigning it. – Barmar Jan 05 '20 at 18:23
  • 1
    Assuming exactly one bit set to 1: `int count = 0; for (int k = FLAG_VAL; k != 1; k >>= 1) ++count;` – Lxer Lx Jan 05 '20 at 18:28

0 Answers0