c - If 语句使用 | 输出运算符(operator)

标签 c

我需要知道如何根据以下代码计算 if 语句的输出 true 或 false:

#include <stdio.h>

enum designFlags {
    BOLD = 8,
    ITALICS = 9,
    UNDERLINE = 4
};

int main() {

    int myDesign = BOLD & ITALICS;
    printf("%d", myDesign);

    if(BOLD & ITALICS)
       printf("Design is ITALIC");
    else
       printf("Design is not ITALIC");

    return 0;
}

最佳答案

首先浏览一下针对运算符的 C 书籍。

如果(粗体和斜体)

'&' 它是按位与运算符。即它对位进行操作

BOLD    = 8 = 0000 1000
                      &
ITALICS = 9 = 0000 1001  (check the truth table of bitwise & )
-------------------------
            = 0000 1000 =8 

因此,if(8) 表示 true,因此会打印“Design is Italics”。

关于c - If 语句使用 | 输出运算符(operator),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47239930/

相关文章:

c - 遗产库

c - 为什么“while(!feof(file))”总是错误的?

我可以在软件中断上下文中使用 free_irq 吗?

c - 两个负数的按位和

c - 管道和 getchar()

c - "POSIX.1-2008 marks ftw() as obsolete"因此意味着什么?

比较字符数组以检查输入是否相同

c - 显示分配的内存量

c - struct itimerspec 作为 timer_create 的参数无效参数

c - 按其中一个字符串值的字母顺序对结构数组进行排序的函数