carduinomoduloarduino-due

Is this a bug in Arduino modulo or is it me?


I created the following simple sketch for my Arduino Due (running 1.6.1) using the modulo operator:

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);
  }
}

The output looks like this:

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

My expectation would be that the remainder value would count up from 0 to 3 again and again. Instead it alternates between 0 for four times and 4 for four times.

I'm completely open to the idea of me doing something wrong, but I can't figure out what it is.


Solution

  • I fail to see a modulo operator (%). I see & instead.