检查寄存器值正在使用C函数设置位

问题描述 投票:0回答:1

我下面有一段代码,用于检查是否设置了寄存器值。

# define My_register_array(index)    (*((P2VAR(uint32, AUTOMATIC, APPL_VAR))Group_register(index)))    /*eg: Group_register(0) address is 0xF3456775*/

# define SET_INIT(index)             ((uint32)1u << (index))

#define MY_CODE


FUNC(boolean, MY_CODE) bo_checkInit(uint32 index)
{
  boolean retVal = FALSE;

  /* Check register is initialized */
  if((My_register_array(index) & SET_INIT(index)) == SET_INIT(index))
  {
    retVal = TRUE;
  }

  return retVal;
}

int main()
{
   boolean retVal = FALSE;
   retVal = bo_checkInit(0); // call function 
   if(retVal)
   {
      printf("register is initialized\n");
   }
   else
   {
     printf("register is not initialized\n");
   }
   
   return 0;
} 

我正在尝试理解这段代码,并想知道它的用途。

if((My_register_array(index) & SET_INIT(index)) == SET_INIT(index))

此代码是否检查寄存器值是否设置为 1?如果有人能解释一下,这将帮助我理解这个概念。

c embedded bit-manipulation bitwise-operators bit-shift
1个回答
0
投票

只要两个操作数都设置了该位,

&
(二进制 AND)的结果就是 1 位,否则为零。

因此,仅当掩码中的所有位(在您的情况下只有一位)也设置在

(X & BitMask) == BitMask
中时,
X
才变为 true。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.