Old 04-08-2008, 05:43 AM   #1
Deric
Human being with feelings
 
Join Date: Mar 2007
Posts: 794
Default 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...
Deric is offline   Reply With Quote
Old 04-08-2008, 06:48 AM   #2
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 6,794
Default

Those don't mean anything by themselves...Can you post more complete code examples?
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass and λ) :
http://xenakios.wordpress.com/
Xenakios is online now   Reply With Quote
Old 04-08-2008, 06:57 AM   #3
Deric
Human being with feelings
 
Join Date: Mar 2007
Posts: 794
Default

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...
Deric is offline   Reply With Quote
Old 04-08-2008, 07:22 AM   #4
Deric
Human being with feelings
 
Join Date: Mar 2007
Posts: 794
Default

i.e.

if (m_name_buttonstates&2)

vs.

if ((m_name_buttonstates&2))

What's the (operational) difference ?
__________________
REAPER? Oh yes...
Deric is offline   Reply With Quote
Old 04-08-2008, 08:15 AM   #5
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 6,794
Default

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...
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass and λ) :
http://xenakios.wordpress.com/

Last edited by Xenakios; 04-08-2008 at 09:52 AM. Reason: lol, extra (
Xenakios is online now   Reply With Quote
Old 04-08-2008, 08:22 AM   #6
Deric
Human being with feelings
 
Join Date: Mar 2007
Posts: 794
Default

Quote:
Originally Posted by Deric View Post
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...
Deric is offline   Reply With Quote
Old 04-08-2008, 09:18 AM   #7
sws
Code Monkey
 
sws's Avatar
 
Join Date: Sep 2007
Location: Madison, WI
Posts: 850
Default

Deric, absolutely no difference.
sws is offline   Reply With Quote
Old 04-08-2008, 09:40 AM   #8
Lawrence
Human being with feelings
 
Join Date: Mar 2007
Posts: 20,949
Default

What he said...
__________________
"I, Bozo The Clown, do solemly swear to uphold the Consti.. consti... uh, how do you pronounce that again?".

Last edited by Lawrence; 04-08-2008 at 09:57 AM.
Lawrence is offline   Reply With Quote
Old 04-08-2008, 09:43 AM   #9
LOSER
Human being with feelings
 
Join Date: May 2006
Posts: 2,373
Default

Quote:
Originally Posted by sws View Post
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 .
LOSER is offline   Reply With Quote
Old 04-08-2008, 09:51 AM   #10
Deric
Human being with feelings
 
Join Date: Mar 2007
Posts: 794
Default

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...
Deric is offline   Reply With Quote
Old 04-08-2008, 09:58 AM   #11
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 10,520
Default

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
Justin is online now   Reply With Quote
Old 04-08-2008, 10:02 AM   #12
Deric
Human being with feelings
 
Join Date: Mar 2007
Posts: 794
Default

Got it - thank you!

Deric.

Ps. And thanks for the written examples.
__________________
REAPER? Oh yes...
Deric is offline   Reply With Quote
Old 04-08-2008, 10:08 AM   #13
LOSER
Human being with feelings
 
Join Date: May 2006
Posts: 2,373
Default

Quote:
Originally Posted by Justin View Post
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).
LOSER is offline   Reply With Quote
Old 04-08-2008, 10:48 AM   #14
sws
Code Monkey
 
sws's Avatar
 
Join Date: Sep 2007
Location: Madison, WI
Posts: 850
Default

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.)
sws is offline   Reply With Quote
Old 04-08-2008, 11:57 AM   #15
LOSER
Human being with feelings
 
Join Date: May 2006
Posts: 2,373
Default

Quote:
Originally Posted by sws View Post
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.
LOSER is offline   Reply With Quote
Old 04-08-2008, 12:12 PM   #16
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 10,520
Default

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.
Justin is online now   Reply With Quote
Old 04-08-2008, 12:17 PM   #17
Deric
Human being with feelings
 
Join Date: Mar 2007
Posts: 794
Default

Quote:
Originally Posted by Justin View Post
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...
Deric is offline   Reply With Quote
Old 04-08-2008, 12:31 PM   #18
sws
Code Monkey
 
sws's Avatar
 
Join Date: Sep 2007
Location: Madison, WI
Posts: 850
Default

Quote:
Originally Posted by Justin View Post
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!
sws is offline   Reply With Quote
Old 04-09-2008, 04:30 AM   #19
LOSER
Human being with feelings
 
Join Date: May 2006
Posts: 2,373
Default

Quote:
Originally Posted by Justin View Post
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?
LOSER is offline   Reply With Quote
Old 04-13-2008, 04:46 PM   #20
Deric
Human being with feelings
 
Join Date: Mar 2007
Posts: 794
Default

Quote:
Originally Posted by Justin View Post
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 View Post
...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.
Deric is offline   Reply With Quote
Old 04-13-2008, 05:14 PM   #21
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 6,794
Default

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?
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass and λ) :
http://xenakios.wordpress.com/
Xenakios is online now   Reply With Quote
Old 04-14-2008, 01:50 AM   #22
Deric
Human being with feelings
 
Join Date: Mar 2007
Posts: 794
Default

Anyone else able to tell me what '!!' means please?

Quote:
Originally Posted by Deric View Post
.../snip/
i.e.

Code:
CSurf_OnArrow(1,!!iszoom);
and:

Code:
if (key == VK_SHIFT) return !!(m_mackie_modifiers&1);
Thanks!
__________________
REAPER? Oh yes...
Deric is offline   Reply With Quote
Old 04-14-2008, 06:27 AM   #23
sws
Code Monkey
 
sws's Avatar
 
Join Date: Sep 2007
Location: Madison, WI
Posts: 850
Default

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;
sws is offline   Reply With Quote
Old 04-14-2008, 06:38 AM   #24
Deric
Human being with feelings
 
Join Date: Mar 2007
Posts: 794
Default

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...
Deric is offline   Reply With Quote
Old 04-14-2008, 07:27 AM   #25
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 9,388
Default

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.
schwa is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 11:50 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2017, vBulletin Solutions Inc.