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.
.
|
|
| User: "Rik G." |
|
| Title: Re: if? |
17 Feb 2005 07:03:33 PM |
|
|
Eh...I mean brackets, parentheses...
.
|
|
|
|
| User: "GB" |
|
| Title: Re: if? |
17 Feb 2005 07:16:50 PM |
|
|
Rik G. wrote:
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
.
|
|
|
|
| User: "Andrey Tarasevich" |
|
| Title: Re: if? |
17 Feb 2005 07:05:44 PM |
|
|
Rik G. wrote:
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.
--
Best regards,
Andrey Tarasevich
.
|
|
|
| User: "Rik G." |
|
| Title: Re: if? |
17 Feb 2005 07:18:20 PM |
|
|
Andrey Tarasevich wrote:
Rik G. wrote:
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.
--
Best regards,
Andrey Tarasevich
Make that: '==' has higher priority than '^'
.
|
|
|
| User: "Andrey Tarasevich" |
|
| Title: Re: if? |
17 Feb 2005 07:42:39 PM |
|
|
Rik G. wrote:
...
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.
Make that: '==' has higher priority than '^'
Oops... Yes, thanks for the correction
--
Best regards,
Andrey Tarasevich
.
|
|
|
|
|
|

|
Related Articles |
|
|