Most efficient way to extract bit flags

后端 未结 5 742
旧巷少年郎
旧巷少年郎 2021-01-02 10:08

I have these possible bit flags.

1, 2, 4, 8, 16, 64, 128, 256, 512, 2048, 4096, 16384, 32768, 65536

So each number is like a true/false sta

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-02 10:57

    One way would be to loop through your number, left-shifting it (ie divide by 2) and compare the first bit with 1 using the & operand.

提交回复
热议问题