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

前端 未结 10 1149
花落未央
花落未央 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条回答
  •  -上瘾入骨i
    2020-12-08 05:46

    Nobody's been wrong so far, but to give a method to check an arbitrary bit:

    int checkBit( byte in, int bit )
    {
      return in & ( 1 << bit );
    }
    

    If the function returns non-zero, the bit is set.

提交回复
热议问题