Reputation:
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.
Upvotes: 2
Views: 442
Reputation:
Ok, thank you all! I've looked at it and it now works as expected. Thanks!
Upvotes: 0
Reputation: 126536
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)
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 newline1, 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 on2.
1technically, 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).
2The 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
Upvotes: 1
Reputation: 58
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:
((sq & 8) && (sq += 7))
'\n'
pieces[board[sq] & 15]
The first part is a condition (if);
(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).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]
).
Upvotes: 3