arduino 中的字节数组操作

问题描述 投票:0回答:1

我有两个像这样的字节数组

byte digitLeftZero[8] = {0b0000,0b0110,0b1001,0b1001,0b1001,0b0110,0b0000,0b0000};
byte digitRightZero[8] = {0b0000,0b0110,0b1001,0b1001,0b1001,0b0110,0b0000,0b0000};

我想将它们合并为

byte digitDoubleZero[8] = {0b00000000,0b01100110,0b10011001,0b10011001,0b10011001,0b01000110,0b00000000,0b00000000};

它基本上是获取第一个数组 0b0000 的第一个元素和第二个数组 0b0000 的第一个元素,然后将它们合并。 0b00000000.

我不知道如何在arduino中操作数组

arrays arduino byte
1个回答
0
投票

您可以使用按位运算符,特别是 Bitshiftleft "<<" and than Bitwiseor "|".

来自 Arduino 文档:

左移位 “...左移运算符 << causes the bits of the left operand to be shifted left by the number of positions specified by the right operand. ..."

按位或 “... C++ 中的按位 OR 运算符是竖线符号 |。与 & 运算符一样,| 独立地运算其周围两个整数表达式中的每一位,但它的作用是不同的(当然)。如果输入位中的一个或两个为 1,则两位为 1,否则为 0。 ..."

变量的类型必须从 byte 更改为 int,但要注意,您必须根据需要在 int 或 unsigned int 之间进行选择。 以二进制表示的 int 具有最高值(最左边),用于定义数字是负数还是正数。

你可以从这样开始:

byte digitLeftZero[8] = {0b0000, 0b0110, 0b1001, 0b1001, 0b1001, 0b0110, 0b0000, 0b0000};
byte digitRightZero[8] = {0b0000, 0b0110, 0b1001, 0b1001, 0b1001, 0b0110, 0b0000, 0b0000};

int a = 0 ;
int b = 0 ;
int c = 0 ;


void setup() {

  // open the serial port at 9600 bps:
  Serial.begin(9600);

  // Loop through arrays
  for (int i = 0; i <= 7; i++) {

    a = digitLeftZero[2] ;
    b = digitRightZero[2] ;

    // debug
    Serial.print(i);
    Serial.print(" - ") ;
    Serial.print(a, BIN);  // print as an ASCII-encoded binary
    Serial.print(" - ") ;
    Serial.print(a << 4, BIN) ;
    Serial.print(" - ") ;

    int c = a << 4 | b ;

    // output as binary representation
    Serial.println(c, BIN);  // print as an ASCII-encoded binary
  }
}

void loop() {

}

Arduino 网站上有一个教程可能很有用。 “使用 Arduino 进行位数学”https://docs.arduino.cc/learn/programming/bit-math

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