简体   繁体   English

C位屏蔽和操作问题

[英]C bit masking AND operation issue

Im new to C and im having trouble understanding the usage of bit masking operations. 我是C语言的新手,无法理解位屏蔽操作的用法。 For example i have the following code 例如我有以下代码

if((input & 0x08)== 0)
{

 //Do....//

}

I took input to be the int value 00000111 , which is 7 in binary and hex, but when i do this comparison it evaluates as false (never entering the if statement). 我将输入值设为int值00000111,该值在二进制和十六进制中为7,但是当我执行此比较时,它的计算结果为false(切勿输入if语句)。

here is what i understand and what should happen 这是我的理解,应该发生什么

          00000111  input
        & 00001000  0x08
         ------------
          00000000  answer

So my question is how can i get the correct output. 所以我的问题是如何获得正确的输出。

int main(){
        int input = 7;

        if((input & 0x08)== 0)
        {
                printf("Hello!\n");
        }

        return 0;
}

You are right, this program prints "Hello!". 是的,此程序将打印“ Hello!”。

There must be another problem in your code. 您的代码中肯定还有另一个问题。 Try always to create functions and test the most simple examples. 始终尝试创建函数并测试最简单的示例。

Try to print the value of input before the if statement. 尝试在if语句之前打印input的值。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM