我使用模运算符为我的 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 次四次之间交替。
我完全接受我做错了什么的想法,但我无法弄清楚它是什么。