質問

Is there a way to improve this line of code? I think this conversion slows down my program. I think something like bit shifting can be used, by I am not sure.

(uint16_t)(0.8*(float)(Value) ?

EDIT: I need to program the atmega8 microcontroller. My teacher said that this line of code would require more processing power and that there is a simpler way of doing this with bit shifting.

役に立ちましたか?

解決

(I'm assuming that Value is an int as well.)

0.8 times x is the same as (4 times x) divided by five. Multiplying an integer by a power of 2 can be done very quickly with a bit-shift. You can do this explicitly if you know how, but any modern compiler will automatically optimize int x = ...; x *= 8; to a bit-shift for you, so you don't need to worry about it. Details about how to do this by hand are widely available if you are interested.

So one thing to try is (Value * 4) / 5.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top