c++ - 如何对 16 位数字执行按位运算 (Arduino)

标签 c++ c arduino bit-manipulation

我正在开发一款由 Arduino 驱动的俄罗斯方 block 游戏。为了跟踪已经掉落并固定的碎片,我有一个字节数组

byte theGrid[] = {   
B00000000,
B00000000,  
B00000000,
B00000000,
B00000000,
...

当井只有 8 个 LED 宽时效果很好,但我需要它是 16 个宽。有没有办法对 16 位数字执行按位运算,比如 short?我尝试只将 theGrid 声明为 short,但无论我做什么,我都会收到此错误。

tetris:62: error: 'B0000000000000000' was not declared in this scope

最佳答案

...leading 'B' only works with 8 bit values (0 to 255)...

来自 http://arduino.cc/en/pmwiki.php?n=Reference/IntegerConstants

只需使用十六进制表示法,即。 2 个字节的 0x0000
0x 表示它是十六进制,每个数字 ( 0123456789ABCDEF ) 替换 4 位。

您可以使用以下代码代替 bitRead 和 bitSet;
变量是 x 和位号 i,i=0 是最右边的位,1 是下一个......):

//set bit to 1
x |= 1<<i;
//set bit to 0
x &= ~(1<<i);
//check if bit is set
if(x & (1<<i))  

例如。 x &= ~(1<<3); 将值 B11111111(二进制表示)设置为 B11110111,
即 0xff 到 0xf7。顺便说一句,x &= ~(1<<3); 等同于 x &= ~8;

关于c++ - 如何对 16 位数字执行按位运算 (Arduino),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27335115/

相关文章:

c++ - 移动速度计算问题

c++ - 没有函数模板如何使用mem_fun_ref?

C 链表追加插入

c -\c 转义序列在 man 中列出但在 C 中未知

c - Arduino线性方程求解器,打印错误答案

c++ - 什么时候会出现结构包装问题?

c++ - fatal error : opencv2/opencv_modules. hpp:没有这样的文件或目录 #include "opencv2/opencv_modules.hpp"

objective-c - 是什么 ? : operator

c++ - Case 语句中的数组

c++ - 未找到 TCS34725 ... 检查您的连接