简体   繁体   English

有人能解释一下这行代码是什么意思吗?

[英]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.第一个参数“%c”表示 printf 需要打印出一个字符。
  • 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.您可以阅读提供的链接,但简而言之,它基本上是 if-else 块的简写。 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);第一部分是条件(if);

  • this expression (sq & 8) uses what is called a bitwise AND operation (read more here ).此表达式(sq & 8)使用所谓的按位 AND 运算(在此处阅读更多内容)。 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.);基本上,二进制中的 8 是 1000,该部分检查 sq 在该位置是否为 1(可以是 1000、11000、101000 等); 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).如果是,则该表达式等于 8(任何大于零的数字都表示为真),如果不是,则等于 0(表示为假)。
  • && means AND, it just means that both left and right expression need to be true && 表示 AND,它只是表示左右表达式都需要为真
  • sq += 7 will add 7 to sq and if it's not 0, it is true. sq += 7会将 7 加到 sq 上,如果它不是 0,则为 true。

The second part \\n is returned (and in your case printed out) if the condition is true;如果条件为真,则返回第二部分\\n (在您的情况下打印出来); else the third part will be printed out ( pieces[board[sq] & 15] ).否则第三部分将被打印出来( 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. printf打印 - 在本例中为" %c ”,它是一个空格和一个字符。 The single character will either be (from the ? - : ternary expression)单个字符将是(来自? - :三元表达式)

  • a newline '\\n'换行符 '\\n'
  • a piece from space sq on the board一个piece从空间sqboard

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. -- 它首先测试sq的单个位( & 8按位执行,并使用一个设置位的常量),如果设置了该位,则将 7 添加到sq并打印换行符1 ,如果未设置,则打印换行符, 将打印该片段。

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) ).可能是在以sq = 0开始并在循环中每次递增sq的循环中(即,类似于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;这在很大程度上取决于board阵列的组织方式——它似乎是一个嵌入了 2D 板的 1D 阵列; the first row at indexes 0..7, the second at indexes 16..23, the third at indexes 32..39 and so on 2 .第一行在索引 0..7 处,第二行在索引 16..23 处,第三行在索引 32..39 处,以此类推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).技术上讲,当该位设置为1时,它会测试加 7 的结果,但除非sq为 -7,否则这将是正确的,这在上下文中可能是不可能的(从 0 开始并且仅从那里递增的循环)。

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. 2这里的间隙是从代码行中的测试推断出来的——那些设置了第 3 位的索引(其中sq & 8为真)不是有效的棋盘空间,而是行之间的“间隙”。 They might be used for something else, elsewhere in the code它们可能用于代码中的其他地方

%c is a format specifier , it means to uderstand this as character %c格式说明符 ,表示将其理解为字符

(expr) ? a : b (expr) ? a : b is a conditional operator that means the following, if expr is true then do a , otherwise do b . (expr) ? a : b条件运算符 ,表示以下内容,如果exprtrue则执行a ,否则执行b

&& is Logical AND operator &&逻辑AND运算符

& here is Bitwise AND operator &这是按位AND运算符

Ok, thank you all!好的,谢谢大家! I've looked at it and it now works as expected.我看过它,现在它按预期工作。 Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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