The Fatal Trap of if(a & 2 == 2) in C Language Operators

#include <stdio.h>

In C/C++ development, the expression if(a & 2 == 2) is an extremely dangerous and common mistake that can lead to a complete deviation from the expected program logic. This seemingly simple expression hides a classic trap related to the operator precedence in the C language.

01ExampleLet’s first look at a set of operation results. On the left is the code, and on the right is the output:The Fatal Trap of if(a & 2 == 2) in C Language OperatorsThe Fatal Trap of if(a & 2 == 2) in C Language OperatorsThe Fatal Trap of if(a & 2 == 2) in C Language OperatorsThe Fatal Trap of if(a & 2 == 2) in C Language OperatorsWhy do these two operators produce completely opposite results when parentheses are added?02Root of the ProblemThe key is the operator precedence!

  • 1. == has a higher precedence than &

  • Therefore, the expression is parsed as:

    a & (2 == 2)

  • That's right, the reason is that simple! Refer to the C/C++ operator precedence table:

  • <img alt="The Fatal Trap of if(a & 2 == 2) in C Language Operators" src="https://boardor.com/wp-content/uploads/2025/10/cb6e515a-a0bb-4925-aabf-e5c670469494.png"/>

Note: The smaller the precedence value, the higher the precedence.

Primary operators ( )、[ ]、->、. are higher than unary operators, which are higher than arithmetic operators (multiplication and division before addition and subtraction), which are higher than relational operators, which are higher than logical operators (excluding !), which are higher than conditional operators, which are higher than assignment operators, which are higher than comma operators.

03Analysis of the ExampleNow let’s revisit our example. The first case is the difference between using & with and without parentheses:

#include <stdio.h>
int main(){
    int a = 2;
    /* When the & expression is in parentheses, it outputs a = 2! */
    if( (a & 0x02) == 0x02 ){
        printf("a = %d! \n", a);
    } else {
        printf("Hello, World! \n");
    }
    // With parentheses, according to the order of operations, first execute a&0x02 = 0x02,
    // then check 0x02 == 0x02, so the result is true, executing the printf in if.
    /* When the & expression is not in parentheses, it outputs Hello, World! */
    if( a & 0x02 == 0x02 ){
        printf("a = %d! \n", a);
    } else {
        printf("Hello, World! \n");
    }
    // First calculate 2 == 2 → result is 1 (true)
    // Then calculate a & 0x01, converting to binary: a=0000 0010,
    // ANDing with 0x01 results in 0 (false), executing the printf in else.
    return 0;
}

Regarding the second case with &&, the reasoning is similar, but why does it yield the opposite result compared to &? We will discuss this when we talk about the difference between & and && next time…

So you may ask, why doesn’t the compiler warn about this?

The reasons are as follows:

Syntax is valid The expression fully complies with C syntax rules
Type matching Both sides are of integer type
Common pattern

The compiler finds it difficult to distinguish whether & 1 is used intentionally

Standards allow The C language standard does not require warnings for such cases

Therefore, when writing code, we must not only pay attention to the absence of error, but also focus on the logical correctness, compliance, operator precedence, and various warnings!

05SummaryFirst, remember a golden rule: for any expression containing bitwise operators and comparison operators, always use parentheses to clarify precedence!Also, pay attention to coding standards:

// Dangerous ❌if(a & MASK == VALUE)// Safe ✅if((a & MASK) == VALUE)

Remember: In embedded systems and safety-critical fields, a missing parenthesis can be worth millions. When you miss typing two characters on the keyboard, you may be laying the groundwork for future disasters.The Fatal Trap of if(a & 2 == 2) in C Language OperatorsThank you for your attention and support. I will update various useful content from time to time. If you have any questions or different opinions, feel free to comment or leave a message for discussion and learning!The Fatal Trap of if(a & 2 == 2) in C Language OperatorsFeel free to follow the public account and give a “share”, “like”, or “view”~Wishing you successful compilation and debugging!

Leave a Comment