General question about code
MichelB
Posts: 154
I don't understand why values of constants are sometimes Hex sometimes Dec. Ex.: SW21-EX26-Servo_Control.BS2
Scale··· CON·· $00C6 (198)
Center· CON·· 1500
PwAdj·· CON·· $0080· (128)
Best regards.
Scale··· CON·· $00C6 (198)
Center· CON·· 1500
PwAdj·· CON·· $0080· (128)
Best regards.
Comments
100 */ $0180
That is equivalent to multiplying 100 by 1.5 ($0100 = 1; $0080 = .5 or 1/2 of 256 )
You could use dec or hex, but using hex for values (constants) when using ** or */ makes it much easier to "read" by imaginging the decimal between the bytes of the word ( */ in "1/256ths") or to the left of the word ( ** in 1/65536ths ).
You "create" a multiplier for */ by doing something like:
x = x * 1.5
x = x */ ( ( 1 * 256 ) + ( .5 * 256 )
x = x */ ( $0100 + $0080 )
x = x */ $0180
In actuality what's happening is that when you use */ you are multiplying the first value by the multplier THEN dividing the result by 256, e.g.
x = ( x * $0180 ) / 256
It's a "trick" for doing fractional math in an integer-only environment. Essentially, you convert everything to "256ths", do your operations then convert back.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
Post Edited (Zoot) : 9/30/2009 4:27:16 PM GMT