JavaBits
JavaBits

Reputation: 2055

Explain the following C++ code part

//class start--

//Global variable

static PMSK *savepr;
static PRC *prs;

//inside some method

static PMSK wkpm;
PMSK *pm;

if (ipf) {
    k = to_bits(312, &msk);     // This will return k=24 and msk =char(00000001),  
    if ( pm->orbits[k] & msk )  // See the answer of my previous question.
        prs[i].pccused = 1; 
}

For the to_bits method pls see the link

Explain the following C++ method

I am not familiar with C++ coding. What is goin on in the second if block? And explain the variable declarations also?

Thanks

Upvotes: 0

Views: 223

Answers (3)

Christian Severin
Christian Severin

Reputation: 1831

If I understand you correctly, you wish to know about the if-clause:

if ( pm->orbits[k] & msk ) contains the bitwise-AND operator, which takes the bits of pm->orbits[k] and the bits of msk and returns those bits that were in both values (that's the "AND" part).

For example:
0010 1101 & 1010 1010 = 0010 1000

EDIT: I suggest you read a good beginners C++ book to learn about pointers (the ->) and arrays (the [k]).

Since you gave no information regarding the PMSK type I have no idea what mp->orbits[k] will give you, apart from this: the PMSK struct or class seems to contain an array called orbits, and pm->orbits[24] denotes its 25th (not the 24th!) element.

Upvotes: 2

Maarten
Maarten

Reputation: 1067

class start--

This is invalid syntax.

static PMSK *savepr;
static PRC *prs;

These are pointers to objects of type PMSK, PRC with internal linkage.

static PMSK wkpm;
PMSK *pm;

An instance of object PMSK with internal linkage and a pointer to a PMSK object "wkpm" with translation-unit scope.

    if(ipf){
        k = to_bits(312, &msk);  // you might want to post this "to_bits" function

    if ( pm->orbits[k] & msk )  // this returns the k+1 object in the array "orbits" and performs a bitwise AND with "msk"
// you might want to post the declaration for this pm instance and the class decaration
        prs[i].pccused = 1;  // this sets the member variable "pcussed" of object i + 1 in the array "prs" to 1
}

Upvotes: 0

Anycorn
Anycorn

Reputation: 51555

if ( pm->orbits[k] & msk ) // check to see if they aare bit-by-bit identical.

And how the variable declarations are goin on? no idea what you mean, clarify.

Upvotes: 1

Related Questions