I was surprised by this code:
if (a =! b) { // let it be on false
...
}
But a is never assigned by a value. What's this operator about?
I was surprised by this code:
if (a =! b) { // let it be on false
...
}
But a is never assigned by a value. What's this operator about?
That's two operators, = and !, not one. It might be an obfuscated way of writing
a = !b;
if (a) {
// whatever
}
setting a to the logical inverse of b, and testing whether the result is true (or, equivalently, whether b was false).
Or it might be a mistyping of a != b.
Long ago, when dinosaurs roamed the earth and C ran on 5th edition UNIX on PDP-11s, =! was the 'not equals' operator. This usage was deprecated by the creation of Standard C, so now it means 'assign the logical inverse', as in a = !b. This is a good argument for always surrounding binary operators with spaces, just to make it clear to the humans reading the code what the compiler is thinking.
I'm a bit surprised nobody else mentioned this, but then again I may be the only SO user to have ever touched a C compiler that old.
a is assigned the boolean negation of b in that line. It is just a misformatted
if( a = !b ) {
... and an evil hidden assignment inside a condition.
a =! b
is just a funny way of putting
a = !b
i.e. the assignment of not b to a.
The value of the expression is a after the assignment.
With the code below you can see that the value of the expression a = !b is !false (i.e. true), and you can then see the assignment has taken place by checking the value of a, which is also true.
#include <iostream>
int main()
{
bool a = false;
bool b = false;
if(a)
printf("a is true!\n");
else
printf("a is false!\n");
if(a = !b)
printf("expression is true!\n");
else
printf("expression is false!\n");
if(a)
printf("a is true!\n");
else
printf("a is false!\n");
}
Result:
a is false!
expression is true!
a is true!
According to C/C++ operators list there is no operator such as =!. However, there is an operator != (Not equal to, Comparison operators/relational operator)
=! operators is in if statement and someone is trying to type != instead of =! because != is the comparison operator which returns true or false.b to a and he/she has done a typo mistake and forgot to put a space after equal sign. This is how the compiler interprets it, anyways.
According to Operator precedence in c++:
!) precedence is 3 and Associativity is Right-to-leftThey are two different operators: the = (assignment) operator together with the ! operator. It can basically be translated to an assignment of a to the negated value of b.
if (a = !b)
But, what the user, probably, meant to write was the != operator:
if (a != b)
That is not a single operator, it is however, a great way to obfuscate code.
If it were written a=!b instead, the white space might not lead you to believe that it was a single operator.
Compilers have warnings for assignment in a conditional expression unless you wrap the entire statement in a set of parenthesis, and this is a perfect example of when this warning would be useful.
Both of these statements are functionally identical, but one generates a warning and the other does not:
if (a =! b) // Generates a warning with `-Wparentheses` (gcc)
if ((a =! b)) // No such warning
-Wparentheses
Warn if parentheses are omitted in certain contexts, such as when there is an assignment in a context where a truth value is expected, or when operators are nested whose precedence people often get confused about.
This, of course, assumes that you are a responsible programmer and actually read the warnings your compiler spits out.
if (a = !b) // Assign A the value of (logical) NOT B and then test the truth
The compiler warning mentioned above is actually useful in the case where the person who wrote this code accidentally transposed ! and =. However, from the cryptic inline comments in your original question, a = !b is probably the author's intention.
C++ does not have an =! operator, which immediately means that this is an = operator followed by a ! operator. So, you simply have a = !b in the if condition.
This is all about clarity of code:
It should be written as: if (a = !b)
if (a = !b) is the same as saying if a is assigned to !b. So there are technically 2 separate operators being used, = which is an assignment operation, and ! which is a logical expression.
Just put a space between = and ! it solves this confusion.
There could be three reasons for this:
It could be a mistyping of the != operator, meaning not equal to. Example:
if (a != b) {
// a is not equal to b
}
It could be a mistyping a == !b, meaning a is equal to not b, which would most commonly be used with booleans. Example:
if (a == !b) {
// The boolean a is equal to not b (a is not equal to b)
}
It could be trying to assign a to the inverse of b. Example:
bool a = !b; // Sets a to the opposite of b
I think this is one of the writing or printing mistake. Because if we can not compile this code as it holds a compiletime error. And here this is inside if clause, the return type must be boolean. So how can it possible. The answer is simply a mistake or as Mike said obfuscated way of writing.
And only possible if both variables a and b are boolean which will be compiled as a is equal to (not)b i.e (a = !b) .
This can be taken as a is assigned to !b.
I am trying to answer your question and, in agreement with Mike's answer, I am just adding up.
You can also understand it by a=!!b.
The code a=!b returns 0 to a and a=!!b returns 1 to a.
The =! is not a single operator but the mix of these two.
These types of question are asked to misguide students to check their coding skills.