|
|
|
04-08-2008, 05:43 AM
|
#1
|
Human being with feelings
Join Date: Mar 2007
Posts: 794
|
What's the difference between...?
...these two please?
(m_name_buttonstates&2)
((m_name_buttonstates&2))
i.e. What is the significance of the extra pair of parentheses?
__________________
REAPER? Oh yes...
|
|
|
04-08-2008, 06:48 AM
|
#2
|
Human being with feelings
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
|
Those don't mean anything by themselves...Can you post more complete code examples?
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.
|
|
|
04-08-2008, 06:57 AM
|
#3
|
Human being with feelings
Join Date: Mar 2007
Posts: 794
|
For sure they don't 'do' anything by themselves - but I am asking what is the difference in the 'logic' (in terms of operation) between these two.
If I post the rest of the statement line - they will be identical - apart from the extra pair of parentheses.
So I am trying to understand the significance/difference the additional parentheses are having on the outcome.
__________________
REAPER? Oh yes...
|
|
|
04-08-2008, 07:22 AM
|
#4
|
Human being with feelings
Join Date: Mar 2007
Posts: 794
|
i.e.
if (m_name_buttonstates&2)
vs.
if ((m_name_buttonstates&2))
What's the (operational) difference ?
__________________
REAPER? Oh yes...
|
|
|
04-08-2008, 08:15 AM
|
#5
|
Human being with feelings
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
|
No difference there, I am quite sure.
The parentheses might do something in a case like this, in Delphi code I would have had to write like this :
if ((m_name_buttonstates&2) && (m_name_buttonstates&8))
But could be in C(++) that isn't necessary, or could even be harmful...
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.
Last edited by Xenakios; 04-08-2008 at 09:52 AM.
Reason: lol, extra (
|
|
|
04-08-2008, 08:22 AM
|
#6
|
Human being with feelings
Join Date: Mar 2007
Posts: 794
|
Quote:
Originally Posted by Deric
i.e.
if (m_name_buttonstates&2)
vs.
if ((m_name_buttonstates&2))
What's the (operational) difference ?
|
Just bumping this question to the top of this thread (in case Justin/Christophe choose to comment) as I'm sure there must be a difference.
Xenakios - have a look at the CSurf code pages - you'll see this sort of thing a lot (both types included - in the same code page). I'm trying to understand this all a bit better - that way I'll be able to implement stuff better... etc!
__________________
REAPER? Oh yes...
|
|
|
04-08-2008, 09:18 AM
|
#7
|
Code Monkey
Join Date: Sep 2007
Location: Madison, WI
Posts: 857
|
Deric, absolutely no difference.
|
|
|
04-08-2008, 09:40 AM
|
#8
|
Human being with feelings
Join Date: Mar 2007
Posts: 21,551
|
What he said...
Last edited by Lawrence; 04-08-2008 at 09:57 AM.
|
|
|
04-08-2008, 09:43 AM
|
#9
|
Human being with feelings
Join Date: May 2006
Posts: 2,373
|
Quote:
Originally Posted by sws
Deric, absolutely no difference.
|
Yeah.
The reason those extra parentheses are there might be that there have been other conditionals. But those were removed, though the parenthese not, or there might be need for additional conditionals to be added later, or whatever.
P.S. Xenakios, you forgot a parentheses in you last post .
|
|
|
04-08-2008, 09:51 AM
|
#10
|
Human being with feelings
Join Date: Mar 2007
Posts: 794
|
I see, thanks for clarifying that LOSER, SWS, and Xenakios.
LOSER - your comment makes some sense - I couldn't understand how/why one person would mix'n'match commands with one/two sets of parentheses for the same-looking command... this led me to believe that there must be an operational difference (despite doing various web searches to no avail).
Thanks to all!
__________________
REAPER? Oh yes...
|
|
|
04-08-2008, 09:58 AM
|
#11
|
Administrator
Join Date: Jan 2005
Location: NYC
Posts: 15,721
|
yeah there's no difference by themselves, i.e. :
Code:
if (x) { }
if ((x)) { }
if (((x))) { }
are all the same. The only time it would matter is if "x" is an expression that could run into precedence issues..
in particular, things like:
Code:
if (somevar & 1 && somevar & 2)
is quite ambiguously defined, it could be interpreted many ways (obviously the compiler has one way of interpreting it, but it may not be the desired)..
-Justin
|
|
|
04-08-2008, 10:02 AM
|
#12
|
Human being with feelings
Join Date: Mar 2007
Posts: 794
|
Got it - thank you!
Deric.
Ps. And thanks for the written examples.
__________________
REAPER? Oh yes...
|
|
|
04-08-2008, 10:08 AM
|
#13
|
Human being with feelings
Join Date: May 2006
Posts: 2,373
|
Quote:
Originally Posted by Justin
are all the same. The only time it would matter is if "x" is an expression that could run into precedence issues..
in particular, things like:
Code:
if (somevar & 1 && somevar & 2)
is quite ambiguously defined, it could be interpreted many ways (obviously the compiler has one way of interpreting it, but it may not be the desired)..
-Justin
|
Operator precedence should be clear and identical among compilers, but it is the human mind that varies. Because who knows that bitwise comes before logic, so no one should be lazy or ashamed to put as many extra parantheses as needed to clarify what is meant with each statement (though don't over do it).
|
|
|
04-08-2008, 10:48 AM
|
#14
|
Code Monkey
Join Date: Sep 2007
Location: Madison, WI
Posts: 857
|
FWIW, the case that is least intuitive is when using bitwise operators with relational/equality ops:
Code:
(var & 4 == 4) // always evaluates to true
((var & 4) == 4) // true if bit 2 set
The rest of the precedence rules "make sense" to me.
(In case you were wondering how big of a geek I am, I have the C operator precendence chart hung up next to monitor.)
|
|
|
04-08-2008, 11:57 AM
|
#15
|
Human being with feelings
Join Date: May 2006
Posts: 2,373
|
Quote:
Originally Posted by sws
FWIW, the case that is least intuitive is when using bitwise operators with relational/equality ops:
Code:
(var & 4 == 4) // always evaluates to true
((var & 4) == 4) // true if bit 2 set
The rest of the precedence rules "make sense" to me.
(In case you were wondering how big of a geek I am, I have the C operator precendence chart hung up next to monitor.)
|
Well I think that the above example is intuitive (at least to me). But I admit I also put the parentheses and I also can see that it isn't really intuitive, expecially since most other operators that calculate something (not saying that bit operations calculate something in a classical mathematical sense or anything though) are above '=='.
So this makes me a geek too cause I argue about operator precedence, hehe.
|
|
|
04-08-2008, 12:12 PM
|
#16
|
Administrator
Join Date: Jan 2005
Location: NYC
Posts: 15,721
|
Quote:
(var & 4 == 4) // always evaluates to true
|
Hmm? wouldn't that be the same as (var&(4==4)) which would be (var&1) ?
Ahh, C and its token-hungriness.
Which makes me think of this ol' question:
Code:
int a=1,b=3;
int c=a+++b;
what happens? (and why?)
-Justin
Last edited by Justin; 04-08-2008 at 12:14 PM.
|
|
|
04-08-2008, 12:17 PM
|
#17
|
Human being with feelings
Join Date: Mar 2007
Posts: 794
|
Quote:
Originally Posted by Justin
Code:
int a=1,b=3;
int c=a+++b;
what happens? (and why?)
-Justin
|
Guess it depends on which of + and ++ take priority in terms of computational-order (i.e. Precedence) in C++.
I guess...
__________________
REAPER? Oh yes...
|
|
|
04-08-2008, 12:31 PM
|
#18
|
Code Monkey
Join Date: Sep 2007
Location: Madison, WI
Posts: 857
|
Quote:
Originally Posted by Justin
Hmm? wouldn't that be the same as (var&(4==4)) which would be (var&1) ?
|
Ah crap Nice case of simple example turned not so simple.
Moral of story - use parenthesis, kids!
|
|
|
04-09-2008, 04:30 AM
|
#19
|
Human being with feelings
Join Date: May 2006
Posts: 2,373
|
Quote:
Originally Posted by Justin
Hmm? wouldn't that be the same as (var&(4==4)) which would be (var&1) ?
Ahh, C and its token-hungriness.
Which makes me think of this ol' question:
Code:
int a=1,b=3;
int c=a+++b;
what happens? (and why?)
-Justin
|
Well postincrement's precedence is higher than that of multiplication, so it will be the same as c = (a++) + b; (methinks)
So I guess its my turn to ask a question.
Code:
int a=1,b=3;
int c= a&&&b;
What happens here?
|
|
|
04-13-2008, 04:46 PM
|
#20
|
Human being with feelings
Join Date: Mar 2007
Posts: 794
|
Quote:
Originally Posted by Justin
Code:
int a=1,b=3;
int c=a+++b;
what happens? (and why?)
|
c=5 As a++ (post-increment) has higher precedence than ++b pre-increment. Therefore c=a+++b; is the same as c=(a+1)+b; Which is also what LOSER stated c=(a++)+b;
Quote:
Originally Posted by LOSER
...my turn to ask a question.
Code:
int a=1,b=3;
int c= a&&&b;
What happens here?
|
Get back to you on that... could be interpreted as an attempt to logical AND a (a&&) with the pointer reference to b (&b) - but I don't know. If so I guess you are trying to associate c with a condition?
Now I'd like to ask a question (no prizes - I don't know the answer) what is: !! (i.e. Two successive exclamation marks)? As in:
Code:
CSurf_OnArrow(1,!!iszoom);
and:
Code:
if (key == VK_SHIFT) return !!(m_mackie_modifiers&1);
Thanks!
__________________
REAPER? Oh yes...
Last edited by Deric; 04-13-2008 at 05:28 PM.
Reason: Added example.
|
|
|
04-13-2008, 05:14 PM
|
#21
|
Human being with feelings
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
|
Hmm, I'd guess it's "Not Not"...But does that make sense? I don't know... A single !-sign means Not, but would not the other one then cancel it out?
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.
|
|
|
04-14-2008, 01:50 AM
|
#22
|
Human being with feelings
Join Date: Mar 2007
Posts: 794
|
Anyone else able to tell me what '!!' means please?
Quote:
Originally Posted by Deric
... /snip/
i.e.
Code:
CSurf_OnArrow(1,!!iszoom);
and:
Code:
if (key == VK_SHIFT) return !!(m_mackie_modifiers&1);
Thanks!
|
__________________
REAPER? Oh yes...
|
|
|
04-14-2008, 06:27 AM
|
#23
|
Code Monkey
Join Date: Sep 2007
Location: Madison, WI
Posts: 857
|
Xen is right, it's "not not". There is a side effect worth nothing; the ! operator typecasts to bool, so !! does the same as (bool).
Code:
bool b;
int i = 10;
// These are all equivalent, sets bool to true
b = !!i;
b = (bool)i;
b = i ? true : false;
// This should give you a compiler warning about
// convering to int from bool
i = !!i;
|
|
|
04-14-2008, 06:38 AM
|
#24
|
Human being with feelings
Join Date: Mar 2007
Posts: 794
|
Erm... sorry (struggling here) I can see that !! could be 'Not Not' - but what is the purpose of Not Not in the examples I've posted (taken from the REAPER control surface SDK)?
I really don't get the point of Not Not...
Regards the Bool usage you mention - I'll have to digest that first.
Thanks very much for answering!
__________________
REAPER? Oh yes...
|
|
|
04-14-2008, 07:27 AM
|
#25
|
Administrator
Join Date: Mar 2007
Location: NY
Posts: 15,747
|
I think "return !!p" is usually slangy code for "return (p == 0 ? false : true);" ... that is, it's used specifically because it converts any nonzero return to true, and casts to bool.
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 04:08 AM.
|