I put this to some exercise:
#define FLP(pin) {PORTD |= 1<<1; PORTD &=~1<<1;}
void setup(void) {
PORTD &=~(1<<1);
DDRD |= (1<<1);
}
const long deviceBits = 10;
const long deviceWordLength = 16;
const long deviceMask = -(1l << (deviceWordLength - 1));
const float deviceScaleFactor = 0.25;
int x=511;
float someFloatVariable;
void loop(void) {
FLP(OUT_PIN); FLP(OUT_PIN); FLP(OUT_PIN); //flip out pin
if (x & deviceMask) {
x |= deviceMask;
}
someFloatVariable = x * (deviceScaleFactor * (1.0/(1 << (deviceWordLength-deviceBits))));
FLP(OUT_PIN); FLP(OUT_PIN); //flip out pin
someFloatVariable = (signed short) ((x & 0x0200)?(x|(-1<<10)):x);
FLP(OUT_PIN);
delay(10);
}
Question: how much time does the floating point approach take than the integer shift approach?
1: less time;
2: the same;
3: 8x;
4: all the above.