How can I check my byte flag, verifying that a specific bit is at 1 or 0?

前端 未结 10 1153
花落未央
花落未央 2020-12-08 05:11

I use a byte to store some flag like 10101010, and I would like to know how to verify that a specific bit is at 1 or 0.

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 05:42

    You can use an AND operator. The example you have: 10101010 and you want to check the third bit you can do: (10101010 AND 00100000) and if you get 00100000 you know that you have the flag at the third position to 1.

提交回复
热议问题