简体   繁体   中英

Could someone explain me what does this line of code mean?

I was wondering what this code really means. I mean, I would like to know what it does, in what order, and what do ? and : signs mean; all explained.

printf(" %c", ((sq & 8) && (sq += 7)) ? '\n' : pieces[board[sq] & 15]);

Thanks.

  • The first argument, " %c", means that printf needs to print out a character.
  • The second argument is the character that the function prints.
  • In this case, the second argument is a ternary operator . You can read the link provided, but in short, it's basically a short-hand for an if-else block. This is what it looks like in your example:

     ((sq & 8) && (sq += 7)) ? '\\n' : pieces[board[sq] & 15]

Let's separate it into three parts:

  1. ((sq & 8) && (sq += 7))

  2. '\\n'

  3. pieces[board[sq] & 15]

The first part is a condition (if);

  • this expression (sq & 8) uses what is called a bitwise AND operation (read more here ). Basically, 8 in binary is 1000 and that part checks whether sq has a 1 in that position (it can be 1000, 11000, 101000 etc.); if it does, that expression equals 8 (any number bigger than zero means true) and if it doesn't, it equals 0 (which means false).
  • && means AND, it just means that both left and right expression need to be true
  • sq += 7 will add 7 to sq and if it's not 0, it is true.

The second part \\n is returned (and in your case printed out) if the condition is true; else the third part will be printed out ( pieces[board[sq] & 15] ).

This is fairly obfuscated code, so it's best to try to understand it in the context in which it appears. By obfuscating it in this way, the auther is trying to tell you "you don't really need to understand the details". So lets try to understand what this does from the 'top down' inferring the details of the context, rather than bottom up.

printf prints -- in this case " %c ", which is a space and a single character. The single character will either be (from the ? - : ternary expression)

  • a newline '\\n'
  • a piece from space sq on the board

which it will be depends on the condition before the ? -- it first tests a single bit of sq (the & 8 does a bitwise and with a constant with one set bit), and if that bit is set, adds 7 to sq and prints the newline 1 , while if it is not set, will print the piece.

So now we really need to know the context. This is probably in a loop that starts with sq = 0 and increments sq each time in the loop (ie, something like for (int sq = 0; ...some condition...; ++sq) ). So what it is doing is printing out the pieces on some row of the board, and when it gets to the end of the row, prints a newline and goes on to the next row. A lot of this depends on how exactly the board array is organized -- it would seem to be a 1D array with a 2D board embedded in it; the first row at indexes 0..7, the second at indexes 16..23, the third at indexes 32..39 and so on 2 .


1 technically, when the bit is set, it tests the result of adding 7, but that will be true unless sq was -7, which is probably impossible from the context (a loop that starts at 0 and only increments from there).

2 The gaps here are inferred from the test in the line of code -- those indexes with bit 3 set (for which sq & 8 will be true) are not valid board spaces, but are instead "gaps" between the rows. They might be used for something else, elsewhere in the code

%c is a format specifier , it means to uderstand this as character

(expr) ? a : b (expr) ? a : b is a conditional operator that means the following, if expr is true then do a , otherwise do b .

&& is Logical AND operator

& here is Bitwise AND operator

Ok, thank you all! I've looked at it and it now works as expected. Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM