Frage

I am trying to make a shift operation to compute y=11*x+7/16*x for 0 < x<2000. My code is as follows: Did I do the shift operation correctly? Also, what does the interval 0 < x<2000 mean? Doesn't x have to be equal to a set constant?

#include <msp430.h> 
int x,y;
void main(void)
{
    y=(x<<3+x<<1+x)+(x<<2+x<<1+x)>>4;
}
War es hilfreich?

Lösung

Notes:

  1. The interval 0 < x < 2000 probably means that you have to iterate on every integer value between 0 and 2000.

  2. From your code, it appears that you want y=11*x+7*x/16 and not y=11*x+7/16*x as stated in your question.

  3. You should add parentheses on every Shift operation, in order to make sure that it is not preceded by an Addition operation.

  4. You can add a printout after every iteration in order to make sure that it works correctly.

See sample code below:

void main(void)
{
    int x,y;
    for (x=1; x<=1999; x++)
    {
        y = ((x<<3)+(x<<1)+x)+(((x<<2)+(x<<1)+x)>>4);
        printf("x=%d y=%d\n",x,y);
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top