Arduino Forum

Using Arduino => Programming Questions => Topic started by: IamFof on Jun 25, 2019, 06:06 pm

Title: Remainder operator query (Resolved)
Post by: IamFof on Jun 25, 2019, 06:06 pm
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?
Title: Re: Remainder operator query
Post by: Romonaga on Jun 25, 2019, 06:25 pm
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.
Title: Re: Remainder operator query
Post by: IamFof on Jun 25, 2019, 06:37 pm
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.
Title: Re: Remainder operator query
Post by: david_2018 on Jun 25, 2019, 06:42 pm
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.
Title: Re: Remainder operator query
Post by: gfvalvo on Jun 25, 2019, 06:45 pm
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?
Title: Re: Remainder operator query
Post by: Romonaga on Jun 25, 2019, 07:05 pm
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.
Title: Re: Remainder operator query
Post by: 6v6gt on Jun 25, 2019, 07:07 pm
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.
Title: Re: Remainder operator query
Post by: IamFof on Jun 25, 2019, 07:56 pm
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.