what is the meaning of this Logical operators combination in C

末鹿安然 提交于 2021-01-29 08:46:41

问题


i know that -> is a pointer |= is OR. what is the logical meaning of such line?

TIMER0->ROUTELOC0 |= TIMER_ROUTELOC0_CC0LOC_LOC15


回答1:


You're ORing in (setting) a value to a register. Your processor has a TIMER0 with a register ROUTELOC0. It likely has a bit that is "CC0LOC_LOC15"

I recommend looking at the data sheet for your processor to figure out what that means specifically.




回答2:


|= does not mean OR. | means OR.

|= is similar to +=, that is

A |= B is the equivalent of A = A | B

So to answer your question: It looks like TIMER0 is a structure with a member ROUTELOC0. The above expression is setting the ROUTELOC0 as the result when ROUTELOC0 is ORed with TIMER_ROUTELOC0_CC0LOC_LOC15




回答3:


The -> is the structure dereference operator, and |= is the bitwise OR assignment operator (compound assignment).

The compound assignment:

x |= y ;

is equavilent to:

x = x | y ;

It is important here to understand that bitwise-OR (|) is distinct from boolean-OR (||). It is used here to set specific bits in x leaving other bits unset.

For example, to set the two least significant bits of x to 1:

    x: 10100000
    y: 00000011
      ---------
  x|y: 10100011


来源:https://stackoverflow.com/questions/61957648/what-is-the-meaning-of-this-logical-operators-combination-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!