What does the expression "!(errS&errE)" do in the if?

Asked

Viewed 94 times

-3

In a piece of code I need to understand, a new syntax appeared to me on if:

if (!(errS&errE)) {
    fprintf(stderr, "\nFALTA ARGUMENTOS\n");
    if(!errS)
        fprintf(stderr, "-s NOME ARQUIVO SAIDA \n");
    if(!errE)
        fprintf(stderr, "-e NOME ARQUIVO ENTRADA \n");
    exit(1);

errS and errE are variable, but I did not understand which condition should be satisfied. The use of ! is strange to me. I know that != is different, but just ! I don’t know. What it means?

2 answers

4


Let’s write the code more readable:

if (!(errS & errE)) {
    fprintf(stderr, "\nFALTA ARGUMENTOS\n");
    if (!errS) fprintf(stderr, "-s NOME ARQUIVO SAIDA \n");
    if (!errE) fprintf(stderr, "-e NOME ARQUIVO ENTRADA \n");
    exit(1);
}

I put in the Github for future reference.

Although in this case it works I consider the first expression evaluated a mistake.

The operator &, in this context, makes a "multiplication" of bits. Then he takes the number contained in each of the variables and individually tests each of them resulting in 1 only when the bit analyzed is 1 in the two variables at the same time. As you probably have a current variable like flag and the only possible values should be 0 and 1 in those variables, it works, because only the least significant bit varies, all others are always 0.

But if this variable were to have values other than 0 and 1, and typing allows this, it would no longer work, or worse, in some cases it would work and others would not. It would already be confusing for an experienced programmer, imagine for a beginner. So it is better to use the correct semantics always. In this case it would be.

if (!(errS && errE))

Thus the number will be guaranteed to be converted to 1 when it is different from 0.

Then the result of the inner expression will only be 1 when the two variables are different from 0.

The ! is a boolean result inverter. So if the result of the expression in parentheses is 1, it will consider the general result as 0 and if the partial result is 0, the end will be 1.

The same goes for the other simpler expressions in which the inversion already occurs directly in the variable. If the variable is 0 the result will be 12, if the variable is anything other than 0 the result will be 0.

Recalling that the if performs whenever the final result of the expression contained in its condition is 1. also remembering that the condition is any expression, even a if (1) can be used, of course meaningless because the if would always perform. Contrary to what many people think it does not need to have a comparison within the if, must have a boolean value, that is, either 0, or 1, as it will be obtained is another question.

0

Do if(!errS) is the same as if(errS == 0), if for example the variable type errS for int. You mean the variable errS is empty. I hope I’ve helped.

  • Thank you very much. But what about "if (!(errS&errE))" ? It means if(errS == erre)?

  • No, it means if(errS == 0 && erre == 0) or if(! errS && ! errr) .

  • "It means that the variable errS is empty" - empty is a vague/ambiguous term for an integer. It is better to say that it has 0 even.

  • The operator & is not equal to the operator &&. The first is for bit-by-bit ("bitwise"), the second for logical operations. https://pt.wikibooks.org/wiki/Programar_em_C/Operadores#Operadores_L%C3%B3gicos_Bit_a_Bit

Browser other questions tagged

You are not signed in. Login or sign up in order to post.