开发者

C++ code translation and explanation

I have the following c++ code snippet. I have a basic understanding of c++ code.Please correct my explanation of the following code where ever necessary:

for (p = q->prnmsk, s = savedx->msk, j = sizeof(q->prnmsk);
           j && !(*p & *s); j--, p++, s++);

What does 开发者_如何学运维it contain: q is char *q(as declared) is type of structure MSK as per code. q->prnmsk contains byte data where prnmask containd 15 bytes.

It is similar for s. So in the for loop as j decreases it will go through each byte and perform this !(*p & *s) operation to continue the loop and eventually if the condition is not met the loop will exit else j will run till j==0.

Am I correct? What does *p and *s mean? Will it contain the byte value?


Some (like me) might think that following is more readable

int j;
for (j = 0; j < sizeof(q->prnmsk); ++j)
{
  if ((q->prnmsk[j] & savedx->msk[j]) != 0) break;
}

which would mean that q->prnmsk and savedx->msk are iterated to find the first occurence of where bit-anding both is not zero. if j equals sizeof(q->prnmsk), all bit-andings were zero.


Yes, you are right. !(*p & *s) means that they want to check if q->prnmsk and savedx->msk don't have corresponding bits set to 1 simultaneously.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜