if?



 DEVELOP > c-Plus-Plus > if?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Rik G."
Date: 17 Feb 2005 06:54:58 PM
Object: if?
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
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER