Like Tree1Likes
  • 1 Post By grumpy

Help with conditional operator.

This is a discussion on Help with conditional operator. within the C Programming forums, part of the General Programming Boards category; 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) ...

  1. #1
    One
    One is offline
    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 03:07 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,342
    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.
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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, 08: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, 02:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21