www.webdeveloper.com
Results 1 to 10 of 10

Thread: Ternary operator

  1. #1
    Join Date
    Dec 2013
    Posts
    3

    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

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355

    Question

    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?


  3. #3
    Join Date
    Aug 2006
    Posts
    1,897
    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.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355
    Quote Originally Posted by tracknut View Post
    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.

  5. #5
    Join Date
    Aug 2006
    Posts
    1,897
    I'm missing the part that's not syntactically correct, but certainly agree it's a nightmare to decipher...

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355
    Quote Originally Posted by tracknut View Post
    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?

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,213
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  8. #8
    Join Date
    Dec 2013
    Posts
    3
    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.

  9. #9
    Join Date
    Aug 2006
    Posts
    1,897
    Quote Originally Posted by JMRKER View Post
    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

  10. #10
    Join Date
    Dec 2013
    Posts
    3
    @tracknut
    I will try your code.
    Thank you!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles