Go Down

Topic: Remainder operator query (Resolved) (Read 219 times) previous topic - next topic

IamFof

Jun 25, 2019, 06:06 pm Last Edit: Jun 25, 2019, 07:57 pm by IamFof
Guys

In some code I was given was this line,

Code: [Select]
if ( (i+1) % 8 == 0  ) Serial.print(' ' ) ;

When I first saw it, I assumed it placed a 'space' every 8 bits (it was printing a bit map).
Trouble was it placed the 'spaces' every 4 bits.

How can (i+1) divided by 8, leave a remainder 0 when (i+1) = 4?

Romonaga

Without seeing what is changing I it is hard to say.  Have you printed the value of I before the if check to validate it has the expected value?

Please post the complete code if you can.

IamFof

There is no problems with the sketch.
The reason for the post was to try and understand what is actually happening in this line.
The variable, i, just counts the bits as it moves along a bit map, prints out the bit state, and every 4 bits prints a 'space'.
After the 4th bit, i will =3, so (i+1) = 4.  Applying the divisor, 8, cannot give me a remainder of 0, any more than if i=3 or i=7.  It can only =0 when (i+1)=8 or 16, etc.

david_2018

Presumably there is something else in the code printing the space at bit #4, or i is being modified in some other way that we cannot see.

gfvalvo

The variable, i, just counts the bits as it moves along a bit map, prints out the bit state, and every 4 bits prints a 'space'.
After the 4th bit, i will =3, so (i+1) = 4.  Applying the divisor, 8, cannot give me a remainder of 0, any more than if i=3 or i=7.  It can only =0 when (i+1)=8 or 16, etc.
You've supplied no code or proof that it behaves as you say. How do you expect to get any help?
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

Romonaga

#5
Jun 25, 2019, 07:05 pm Last Edit: Jun 25, 2019, 07:07 pm by Romonaga
There is no problems with the sketch.
The reason for the post was to try and understand what is actually happening in this line.
The variable, i, just counts the bits as it moves along a bit map, prints out the bit state, and every 4 bits prints a 'space'.
After the 4th bit, i will =3, so (i+1) = 4.  Applying the divisor, 8, cannot give me a remainder of 0, any more than if i=3 or i=7.  It can only =0 when (i+1)=8 or 16, etc.

Clearly there is something wrong with the code.  We all agree the modulus operator is not returning what you expect.  This indicates the value i is not what you expect.  Add a simple print statement will prove this to be true or false.  However, we can not assess what is wrong with that little snippit of code.

6v6gt

#6
Jun 25, 2019, 07:07 pm Last Edit: Jun 25, 2019, 07:09 pm by 6v6gt
The code is here (supplied originally by me)

Code: [Select]

    // print raw data
    for ( uint8_t i = 0 ; i < 32 ; i++ )   {
      Serial.print( bitRead( nsIrNec::dataRaw, i ) ) ;
      if ( (i+1) % 8 == 0  ) Serial.print(' ' ) ;
    }
    Serial.println();


and is intended to print something like this, depending on the sample data:

00000000 11111111 11101010 00010101

nsIrNec::dataRaw is of type uint32_t

The '+1' is to ensure that the first space is after digit 7 (digits are numbered 0 to 7) is printed.
Without the '+1' the first space would appear after the first digit.

IamFof

Guys

What can I say, other than to plead insanity.

My query was why it spaced every 4.  I've just run the sketch again, and it spaces, as expected, every 8.

My brain & I must, somehow, have suffered a disconnect resulting in nibbles becoming invisible.

Sorry.

Go Up