Checking if bit is not set

前端 未结 4 1037
挽巷
挽巷 2020-12-31 07:30

If I use this: if(value & 4) to check if the bit is set, then how do I check if the bit isn\'t set?

I tried with if(!value & 4) or

4条回答
  •  借酒劲吻你
    2020-12-31 08:03

    When you write if(value & 4), C checks the result to be non-zero. Essentially, it means

    if((value & 4) != 0) {
        ...
    }
    

    Therefore, if you would like to check that the bit is not set, compare the result for equality to zero:

    if((value & 4) == 0) {
        ...
    }
    

提交回复
热议问题