检查是否位未设置 [英] Checking if bit is not set

查看:103
本文介绍了检查是否位未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我用这个:
如果(价值和4)?来检查,如果该位被设置,那么我该如何检查,如果该位未设置

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?

我试着用
如果(Valie酒店&安培;!4)如果(〜值及4)如果(价值^ 4)但他们没有工作。

I tried with if(!valie & 4) or if(~value & 4) and if(value ^ 4) but none of them works.

推荐答案

当你写如果(价值及4),C检查结果为非零。本质上,这意味着

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) {
    ...
}

这篇关于检查是否位未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆