Thread: Help with conditional operator.

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Help with conditional operator.

    Hi,

    Usually I understand how to use the conditional operator in C, but this one was tricky.
    Code:
    if (GEN_SLIDING_ATTACKS[(RANKS[square]-1) < (FILES[square]-1) ? (RANKS[square]-1) : (FILES[square]-1)][state6Bit] & CHARBITSET[attackbit])
    GEN_SLIDING_ATTACKS[..... is an two-dimensional array, while RANKS[... and FILES[... is two one-dimensional arrays.

    Could someone please explain to me where both the true and the false part in the statement ends up?
    --
    One
    Last edited by One; 12-11-2012 at 04:07 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The ternary operator is being used to compute the first index.

    So (assuming index is of an appropriate integral type) the code is equivalent to
    Code:
        /*   first expand the ternary operator */
     
        if ((RANKS[square] - 1) < (FILES[square] - 1))
             index = RANKS[square] - 1;
        else
             index = FILES[square] - 1;
    
        /*   now plug that into the rest of the if() statement  */
    
           
    
        if (GEN_SLIDING_ATTACKS[index][state6Bit]  & CHARBITSET[attackbit])
              /* whatever */
    Note that I've done a literal interpretation of the code .... plenty of opportunities for making it simpler.

    Whoever wrote that code was more interested in getting everything into one statement, than in clarity of code. IMHO, that has demonstrated their foolishness, no more.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Somewhat simplification: if (GEN_SLIDING_ATTACKS[min(RANKS[square], FILES[square]) - 1][state6Bit] & CHARBITSET[attackbit])

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conditional Operator
    By Waqas Asad in forum C Programming
    Replies: 4
    Last Post: 12-19-2011, 09:11 AM
  2. conditional operator
    By nasser in forum C Programming
    Replies: 2
    Last Post: 10-23-2011, 07:18 PM
  3. Conditional Operator
    By markcocoa10 in forum C Programming
    Replies: 7
    Last Post: 10-15-2010, 06:22 PM
  4. Conditional Operator
    By arjunajay in forum C Programming
    Replies: 8
    Last Post: 07-10-2008, 08:17 AM
  5. Conditional operator ? :)
    By ER in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2001, 03:34 AM