JavaBits
JavaBits

Reputation: 2055

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?

Upvotes: 1

Views: 140

Answers (2)

stefaanv
stefaanv

Reputation: 14392

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.

Upvotes: 3

vines
vines

Reputation: 5225

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.

Upvotes: 1

Related Questions