简体   繁体   中英

Defijne meaning of statement in C++

I am working with some C++ code and I am just a novice and I do not understand what this conditional statement means for a true or false result.

This is what I have:

Font contains values related to bitmap font

for(j = 0; j < COUNT; j++)   {
    for(i = 0; i < 8; i++)  {
        Font[j][i] <<= 1;
        if((j != COUNT -1) && (Font[j + 1][i] & 0x80))
            Font[j][i] |= 0x01;
    }
}

I understand most of this, the Boolean && , then the & is confusing me for relevancy in this use and then the lone 0x80 , I just don't understand how that relates to the (Font[j + 1][i] & 0x80) 0x80 is 128 decimal... The font is a 128 x 8 font , but is that a relationship? Can someone help me put this together so that I can understand how it is providing the condition? I also need to know how |= 0x01 affects Font[j][i]. Is that a form of piping?

The if statement generic format is

if (conditional_expression) ...

The conditional_expression is any expression which yields a result. The result zero ( 0 ) is false, and anything non-zero is true.

If the conditional_expression is simple and without any kind of comparison, then it's implicitly compared to zero. For example

int my_variable = ...;  // Actual value irrelevant for example

if (my_variable) { ... }

The if statement above is implicitly equal to

if (my_variable != 0) { ... }

This implicit comparison to zero is also done for compound conditions, for example

if (some_condition && my_variable) { ... }

is equal to

if (some_condition && my_variable != 0) { ... }

Now we get back to your code and the condition:

if((j != COUNT -1) && (Font[j + 1][i] & 0x80))

With the implicit comparison against zero, the above is equal to

if((j != COUNT -1) && (Font[j + 1][i] & 0x80) != 0)

That is, the right-hand side of the && check is Font[j + 1][i] & 0x80 is zero or not.

As for the & operator itself, it's the bitwise AND , and in essence can be used to check if a bit is set or not. For your code it checks if the bit corresponding to the value 0x80 (the eight bit) is set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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