这是 Arduino 模块中的错误还是我?
Is this a bug in Arduino modulo or is it me?
我使用模运算符为我的 Arduino Due (运行 1.6.1) 创建了以下简单草图:
int count = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Count: ");
Serial.println(count);
Serial.print("Count / 4 = ");
Serial.println(count / 4);
Serial.print("Remainder = ");
Serial.println(count & 4);
Serial.println();
count++;
if (count == 50) {
delay(86400000);
} else {
delay(1000);
}
}
输出如下所示:
Count: 0
Count / 4 = 0
Remainder = 0
Count: 1
Count / 4 = 0
Remainder = 0
Count: 2
Count / 4 = 0
Remainder = 0
Count: 3
Count / 4 = 0
Remainder = 0
Count: 4
Count / 4 = 1
Remainder = 4
Count: 5
Count / 4 = 1
Remainder = 4
Count: 6
Count / 4 = 1
Remainder = 4
Count: 7
Count / 4 = 1
Remainder = 4
Count: 8
Count / 4 = 2
Remainder = 0
Count: 9
Count / 4 = 2
Remainder = 0
Count: 10
Count / 4 = 2
Remainder = 0
Count: 11
Count / 4 = 2
Remainder = 0
Count: 12
Count / 4 = 3
Remainder = 4
Count: 13
Count / 4 = 3
Remainder = 4
Count: 14
Count / 4 = 3
Remainder = 4
Count: 15
Count / 4 = 3
Remainder = 4
Count: 16
Count / 4 = 4
Remainder = 0
我的期望是余数会一次又一次地从 0 增加到 3。相反,它在 0 表示四次和 4 表示四次之间交替。
我对我做错事的想法完全开放,但我无法弄清楚它是什么。
我没看到模运算符 (%
)。我看到的是 &
。
我使用模运算符为我的 Arduino Due (运行 1.6.1) 创建了以下简单草图:
int count = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Count: ");
Serial.println(count);
Serial.print("Count / 4 = ");
Serial.println(count / 4);
Serial.print("Remainder = ");
Serial.println(count & 4);
Serial.println();
count++;
if (count == 50) {
delay(86400000);
} else {
delay(1000);
}
}
输出如下所示:
Count: 0
Count / 4 = 0
Remainder = 0
Count: 1
Count / 4 = 0
Remainder = 0
Count: 2
Count / 4 = 0
Remainder = 0
Count: 3
Count / 4 = 0
Remainder = 0
Count: 4
Count / 4 = 1
Remainder = 4
Count: 5
Count / 4 = 1
Remainder = 4
Count: 6
Count / 4 = 1
Remainder = 4
Count: 7
Count / 4 = 1
Remainder = 4
Count: 8
Count / 4 = 2
Remainder = 0
Count: 9
Count / 4 = 2
Remainder = 0
Count: 10
Count / 4 = 2
Remainder = 0
Count: 11
Count / 4 = 2
Remainder = 0
Count: 12
Count / 4 = 3
Remainder = 4
Count: 13
Count / 4 = 3
Remainder = 4
Count: 14
Count / 4 = 3
Remainder = 4
Count: 15
Count / 4 = 3
Remainder = 4
Count: 16
Count / 4 = 4
Remainder = 0
我的期望是余数会一次又一次地从 0 增加到 3。相反,它在 0 表示四次和 4 表示四次之间交替。
我对我做错事的想法完全开放,但我无法弄清楚它是什么。
我没看到模运算符 (%
)。我看到的是 &
。