# Ternary operator

• 12-23-2013, 04:41 PM
JRoberto
Ternary operator
Hi

Can someone help me to translate this code:
Code:

return  ( ( ayr[7] >> ch  | tons >> ch )
& ( ayr[7] >> ch+3 | noiv      )
& 1 )  * amp[ ayr[8+ch] & 0x10
? envv
: ayr[8+ch] & 0x0f ];

in to IF ELSE expression?

Thank´s
• 12-23-2013, 06:12 PM
JMRKER
Can you expand on what is supposed to do?

Also, the AND logical comparison is && not a single & character.
And the logical OR is || not | only.
Is your >> supposed to be some sort of right shift operation?

:confused:
• 12-23-2013, 06:35 PM
tracknut
Not that it's any easier to read:
Code:

if (ayr[8+ch] & 0x10)
return (( ( ayr[7] >> ch  | tons >> ch )
& ( ayr[7] >> ch+3 | noiv      )
& 1 )  * amp[envv];
else
return ( ( ayr[7] >> ch  | tons >> ch )
& ( ayr[7] >> ch+3 | noiv      )
& 1 )  * amp[ayr[8+ch] & 0x0f];

JRMKR, those are all bitwise and's and or's and shifts.
• 12-23-2013, 06:52 PM
JMRKER
Quote:

Originally Posted by tracknut
Not that it's any easier to read:
Code:

if (ayr[8+ch] & 0x10)
return (( ( ayr[7] >> ch  | tons >> ch )
& ( ayr[7] >> ch+3 | noiv      )
& 1 )  * amp[envv];
else
return ( ( ayr[7] >> ch  | tons >> ch )
& ( ayr[7] >> ch+3 | noiv      )
& 1 )  * amp[ayr[8+ch] & 0x0f];

JRMKR, those are all bitwise and's and or's and shifts.

That was my point. It was not syntax correct.
Still does not make it any easier to decipher without some comments.
I would think 2 weeks away from the code will not make it any easier to decipher than a 6 month hiatus.
• 12-23-2013, 06:57 PM
tracknut
I'm missing the part that's not syntactically correct, but certainly agree it's a nightmare to decipher... :)
• 12-23-2013, 07:23 PM
JMRKER
Quote:

Originally Posted by tracknut
I'm missing the part that's not syntactically correct, but certainly agree it's a nightmare to decipher... :)

I'm referring to the original post.

Note also that '&' alone is supposed to do what in your post?
• 12-23-2013, 07:47 PM
\\.\
• 12-23-2013, 08:06 PM
JRoberto
Code:

function cstep(ch) {
if( ++ayr[ch+16] >= (ayr[ch<<1] | ayr[1|ch<<1]<<8) )
ayr[ch+16]= 0,
tons^= 1 << ch;
return  ( ( ayr[7] >> ch  | tons >> ch )
& ( ayr[7] >> ch+3 | noiv      )
& 1 )  * amp[ ayr[8+ch] & 0x10
? envv
: ayr[8+ch] & 0x0f ];
}

This is the complete function that i'm translating to Purebasic syntax.
The first part is not so hard to undesrtand, but i'm very confusing about the 'return ' part. With IF ELSE statements is more easy to understand.
Mostly are bitwise operations.
• 12-23-2013, 08:08 PM
tracknut
Quote:

Originally Posted by JMRKER
I'm referring to the original post.

Note also that '&' alone is supposed to do what in your post?

Whether the OP or the one I provides, here's an English version for this:
Code:

return (( ( ayr[7] >> ch  | tons >> ch )
& ( ayr[7] >> ch+3 | noiv      )
& 1 )  * amp[envv];

"take the value of (( ayr[7] >> ch | tons >> ch) and bitwise and it with (ayr[7] >> ch+3 | noiv ) and bitwise and the result with 1 (i.e. get the value of the low order bit). Multiply the result by amp[envv], and return that value. So the returned value will either be zero or amp[envv], depending on the low order bit of the result of all the shifting, or'ing and and'ing"

...and that's only the first half of it :)
• 12-23-2013, 08:10 PM
JRoberto
@tracknut
I will try your code.
Thank you!

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.24569 seconds
• Memory Usage 2,377KB
• Queries Executed 11 (?)
Template Usage (21):
• (5)bbcode_code_printable
• (3)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates