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 I want but I thought the extra braces weren't
necessary. Or are they?

R.
 
A

Andrey Tarasevich

Rik 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 I want but I thought the extra braces weren't
necessary. Or are they?
...

'^' has higher priority than '==', which means that these tests do
different things. The first one is equivalent to

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

Apparently, that's not what you need.
 
G

GB

Rik 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 I want but I thought the extra braces weren't
necessary. Or are they?

R.

Look up operator precedence in your C++ reference. Here is one online (I
have not checked it to see if it is correct):

http://www.cppreference.com/operator_precedence.html

The equality operator has higher precedence than the xor operator, so
the first example is xoring with the results of the equality test.

Gregg
 
R

Rik G.

Andrey said:
'^' has higher priority than '==', which means that these tests do
different things. The first one is equivalent to

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

Apparently, that's not what you need.

Make that: '==' has higher priority than '^'
 

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

Forum statistics

Threads
474,184
Messages
2,570,981
Members
47,582
Latest member
brooksmith

Latest Threads

Top