简体   繁体   English

我如何解释 << 和 | 在 C

[英]How can I interpret << and | in C

I need help to understand what is happening in this declaration:我需要帮助来了解此声明中发生的事情:

#define LDA(m)  (LDA_OP << 5 | ((m) & 0x001f))

Thank you谢谢

y << x is a left shift of y by x . y << x是一个左移位y通过x x & y is a bitwise and of x and y . x & y是一个按位和xy

So, the left shift operator is like multiplying by 10 in base 10, but instead you multiply for 2 in base 2, for example:因此,左移运算符就像在以 10 为底乘以 10 一样,但在以 2 为底乘以 2,例如:

In base 10在基地 10

300 * 10 = 3000 300 * 10 = 3000

In base 2:在基数 2 中:

0b0001 * 2 = 0b0010 = 0b0001 << 1 0b0001 * 2 = 0b0010 = 0b0001 << 1

with a << b you "push" the number a, b places to the left.使用 a << b 您“推”数字 a, b 向左放置。

and the or operator ( | ) you have to take two bits and if one or both of them are true (1) then the result is true.和 or 运算符( | )你必须取两位,如果他们中的一个或两个都为真(1),那么结果为真。

For example:例如:

0b0010 | 0b0010 | 0b0001 = 0b0011 0b0001 = 0b0011

0b0010 | 0b0010 | 0b0010 = 0b0010 0b0010 = 0b0010

If you have problems with this operators, just try to work the same numbers but in binary.如果您对此运算符有疑问,请尝试使用相同的数字但以二进制形式处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM