By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

if?

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Eh...I mean brackets, parentheses...
Jul 23 '05 #2

P: n/a
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
Jul 23 '05 #3

P: n/a
GB
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
Jul 23 '05 #4

P: n/a


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 '^'
Jul 23 '05 #5

P: n/a
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
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.