Reputation: 75
I am pretty confused by a library doing something similar to:
while ( func() & DEFINED_CONST == DEFINED_CONST )
{
...
}
I am compiling C99 strict ansi. I thought that precedence of ==
is higher than &
. If this is the case the evaluation of the DEFINED_CONST == DEFINED_CONST
would be first. This would be totally nonsense. Since this example comes from a company which I think will not provide such bullshit, I am asking me, where is my lag of knowledge, what do I get wrong.
It would make sense to do: (func() & DEFINED_CONST) == DEFINED_CONST
, but since the precedence of ==
is higher I thought it would get evaluated like func() & (DEFINED_CONST == DEFINED_CONST)
.
So what do I get wrong.
For background, func()
is reading an register.
Upvotes: 0
Views: 184
Reputation: 67546
if you compile yor code the compiler will tell you what the problem is :https://godbolt.org/z/61edYq
At the moment this expression checks if LSBit is set in the value returned by func.
What you can do:
while ( (func() & DEFINED_CONST) == DEFINED_CONST )
while ( func() & DEFINED_CONST)
Upvotes: 2
Reputation: 229108
Yes, you are right, that code is probably wrong and it is the same as
func() & (DEFINED_CONST == DEFINED_CONST)
Likely the code should be
(func() & DEFINED_CONST) == DEFINED_CONST
Upvotes: 2