(1 << 5); I am having problem understand.

问题描述 投票:0回答:1
What is the logical meaning of such C operator combinations in single line?

The following code configures pin 5 from Port C as a digital input with pull-up and filter. The MODE5 bitfield from GPIO_PC_MODELregister must be set to 0b0011, and the correspondent bit in the GPIO_PC_DOUT register must also be set to 1 to determine the pulldirection. A pin can be configured either by using the functions available in the emlib or through a direct register write:

  • ->

  • |

  • .DOUTFor EFR32FG14P231F256M32 i have the following combination of C operators. with the descrition shown bellow.I am having trouble matching the description to the code. -> is a pointer. we have pointing ...DOUT is a constant expression having the binary value P[gpioPortC]. You could simply replace it with the literal constant value 0x20 or 32, however the purpose of the expression is to create a

bit mask

with bit-5 set.

It is expressed in this manner because:

GPIO->P[gpioPortC ].DOUT = GPIO->P[gpioPortC ].DOUT | (1 << 5); //Input enabled with pull up and filter
GPIO->P[gpioPortC ].MODEL = (GPIO->P[gpioPortC ].MODEL & ~_GPIO_P_MODEL_MODE5_MASK) |
GPIO_P_MODEL_MODE5_INPUTPULLFILTER;
it documents intent without having to write a comment. It is self-documenting code - it says to the reader
c embedded operators
1个回答
0
投票

1 << 5when coding it, it is less error prone to let the compiler calculate the constant value of a bit 5 mask.0b100000So:sets bit 5 of the variable (or in this case register

)

  • . With respect to your previous question, it can also be expressed as:
  • With respec to your secondary question:

x = x | ( 1 << 5) ;

is a pointer. We have pointing directions on both sides?x I am not clear what you are asking, but is not a pointer; it is the

x |= (1 <<5) ;

structure dereference

operator; its left-hand operand is a pointer, and its right hand operator is a member of the structure to which the left-hand is a pointer
    to
  • -> (which may or may not be a pointer.And before you ask, the second line of your fragment, that has teh
  • form

:->Sets all the bits of that are 1 in to zero. It is the revers of the previous idiom:In your particular example you have a more complex expression:Here you need to understand the concept of

operator precedence. has precedence over

x = x & ~mask ;

, so the x sub expression is evaluatet first so that the mask applies to the

 x &= ~mask ;  // Clear mask bits
 x &= mask ;   // Set mask pits

member of the referenced structure. Note that

GPIO->P[gpioPortC].DOUT | (1 << 5)

is also an operator, (with higher precedence than . So that in the general form:...-> | GPIO->P[gpioPortC].DOUTThe << operator means "shift bits left", and it works as follows:| (1 << 5);shifts the bits in DOUTa[], ->b

x | y ;

places to the left.to answer your question directly, 1 << 5 moves the bits in 0x01 5 times to the left, resulting in 0x20, which is 32 in decimal notation. This has the effect of multiplying a number by 2 n times (n=5 in your case). The resultant value is ORed with the value in DOUT, and then stored in DOUT itself.

x =  GPIO->P[gpioPortC].DOUT 
y = (1 << 5)
EDIT:To answer the question OP added in the comments -

1
投票

.

a << b

The operator is shorthand for . The first line in your code instructs the following:Take the value within and OR it with

.

Assign the result to x = x + 5.

The second line does a pretty similar operation, only instead of preforming a bitwise OR and storing it in the same field, it preforms a bitwise AND of the field with a negated form of some predefined value, and then preforms a bitwise OR of the result with another predefined value, and then stores it within that field.->It is important to understand that simply using the (*obj).field operator on an object does nothing except allowing you to access the fields stored within this object (which themselves might be pointers to other locations in memory, or primitive variables).

  1. GPIO->P[gpioPortC].DOUT对于EFR32FG14P231F256M32,我有以下的C运算符组合。0x10 是一个指针。我们两边都有指向性?
  2. 是位元ORGPIO->P[gpioPortC].DOUT 我想是指把

以下成员

细胞。->但改级

© www.soinside.com 2019 - 2024. All rights reserved.