Unexpected results?

12 Dec 2010 . Edited: 12 Dec 2010

Two procedures that should give identical results.

However the the second one does not work,gives unexpected results.

//Module data
bool bitdata[30]; 
char digdata[10];
int bitNr;
int digNr;

//Convert bitdata into digdata
void make_digdata(){
    bitNr=0;
    for (digNr = 0;digNr < 7;digNr++)
    {
        digdata[digNr] = (bitdata[bitNr+0] * 8) + (bitdata[bitNr+1] * 4) + (bitdata[bitNr+2] * 2) + (bitdata[bitNr+3]);
        bitNr += 4;
    }
}

//Module data
bool bitdata[30]; 
char digdata[10];
int bitNr;
int digNr;

//Convert bitdata into digdata
void make_digdata1(){
    bitNr=0;
    for (digNr = 0;digNr < 7;digNr++)
    {
        digdata[digNr] = (bitdata[bitNr++] * 8) + (bitdata[bitNr++] * 4) + (bitdata[bitNr++] * 2) + (bitdata[bitNr++]);
    }
}

I thought that bitNr was used as index first and than evaluated (incremented)

 

12 Dec 2010

AFAIK the order of evaluation of the expression terms is implementation-defined. What I suspect happens is that compiler starts to evaluate sum terms from the right, so you get bitdata[bitNr], then bitdata[bitNr+1]*2, etc. It's best not to use the increment operator more than once in a statement.

13 Dec 2010

 

when in doubt, use parentheses!

14 Dec 2010

 

Thanks for your reponse.

The first methode wil be used.