if?

R

Rik G.

Should there be a difference in these if tests?

1. if ( (dwRC + 123456789) ^ dwEK == dwUC ) {}

2. if ( ((dwRC + 123456789) ^ dwEK) == dwUC ) {}

Only 2. works as intended but I thought the extra brackets weren't
necessary. Or are they?

R.
 
R

Rik G.

Rik G. said:
Should there be a difference in these if tests?

1. if ( (dwRC + 123456789) ^ dwEK == dwUC ) {}

2. if ( ((dwRC + 123456789) ^ dwEK) == dwUC ) {}

Only 2. works as intended but I thought the extra brackets weren't
necessary. Or are they?

R.

OK leave it: '^' has higher priority than '=='

R.
 
C

Chris Croughton

I mean: '==' has higher priority than '^'

Yes, you've fallen into the silly part of the precedence table (I have
never seen a program which used the bit operators on the results of
comparisons, but many using comparisons on the results of bit
operators). Unfortunately it's been there since the earliest versions
of C and there is no chance of changing it to something reasonable now.

Chris C
 
S

SM Ryan

# Should there be a difference in these if tests?
#
# 1. if ( (dwRC + 123456789) ^ dwEK == dwUC ) {}

This is equivalent to
if ( (dwRC + 123456789) ^ (dwEK == dwUC) ) {}

# 2. if ( ((dwRC + 123456789) ^ dwEK) == dwUC ) {}
#
# Only 2. works as intended but I thought the extra brackets weren't
# necessary. Or are they?

I don't know what your intentions are. If you want to XOR the results of equality
to the sum, (1) is correct. If you want to XOR the sum and then compare, (2) is
correct.

Ritchie has since opined that the precedence of & | and ^ are misplaced, but it's
too late to change that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top