对数组的索引进行位屏蔽,Arduino 环境

发布于 2024-07-27 04:35:16 字数 250 浏览 1 评论 0原文

我试图从结构中获取一个 16 位无符号整数,屏蔽它的前 8 位,并将其用作带有函数 AnalogWrite 的数组的索引,该函数将 Arduino 上的输出引脚和输出字节作为参数。 有问题的代码如下所示: analogWrite(outputPin, my_array[myStructure->data & 0xFF00 >> 8]);

不幸的是,此代码不起作用。 我总是得到零作为输出。 有任何想法吗?

I'm trying to take a 16 bit unsigned integer from a structure, mask the first 8 bits of it, and use it as an index to an array with the function analogWrite which takes the output pin on the Arduino and the output byte as arguments. The code in question looks something like this: analogWrite(outputPin, my_array[myStructure->data & 0xFF00 >> 8]);

Unfortunately, this code doesn't work. I always get zero as an output. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路弥 2024-08-03 04:35:16

运算符优先级

这:

myStructure->data & 0xFF00 >> 8

相当于:

myStructure->data & (0xFF00 >> 8)

但你想要:

(myStructure->data & 0xFF00) >> 8

Operator precedence.

This:

myStructure->data & 0xFF00 >> 8

Is equivalent to:

myStructure->data & (0xFF00 >> 8)

But you want:

(myStructure->data & 0xFF00) >> 8
熟人话多 2024-08-03 04:35:16

我怀疑运算符优先级。 尝试:

analogWrite(outputPin, my_array[(myStructure->data & 0xFF00) >> 8]);

I suspect operator precedence. Try:

analogWrite(outputPin, my_array[(myStructure->data & 0xFF00) >> 8]);
苄①跕圉湢 2024-08-03 04:35:16

查看 C 的优先级表,您执行以下操作:

  1. 从 myStructure 获取数据

  2. 右移0xFF00 8步

  3. 按位及其数据

    使用更多行或括号!

Looking at a precedence table for C you do the following:

  1. Get data from myStructure

  2. Right-shift 0xFF00 8 steps

  3. Bit-wise and it with data

    Use more rows or parenthesis!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文