位操作-解释下面的c++代码部分

bit manipulation - Explain the following C++ code part

本文关键字:c++ 代码部 解释 位操作      更新时间:2023-10-16
//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; 
}
关于to_bits方法,请参见链接

解释下面的c++方法

我不熟悉c++编程。第二个if块中发生了什么?解释一下变量声明?

谢谢

如果我理解正确的话,您是想了解if -clause:

if ( pm->orbits[k] & msk )包含位与运算符,该运算符取pm->orbits[k]的位和msk的位,并返回两个值中的位(即"与"部分)。

例如

:
0010 1101 &1010 1010 = 0010 1000

编辑:

我建议你读一本好的c++初学者书籍来学习指针(->)和数组([k])。

因为你没有给出关于PMSK类型的信息,我不知道mp->orbits[k]会给你什么,除了这个:PMSK结构或类似乎包含一个名为轨道的数组,pm->orbits[24]表示它的第25个(不是第24个!)元素。

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

变量声明是如何进行的?我不明白你的意思,请说清楚。

class start--

语法无效。

static PMSK *savepr;
static PRC *prs;

这些指针指向具有内部链接的PMSK, PRC类型的对象。

static PMSK wkpm;
PMSK *pm;

具有内部链接的PMSK对象实例和指向具有平移单位作用域的PMSK对象"wkpm"的指针。

    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
}