Quote:
|
Originally Posted by AIBob
abs(p1_x) can be replaced with (p1_x && 0x7F), because the last bit is the sign,...
|
Not so. For two reasons, the first of which is very important:
Negative numbers are not stored in sign-magnitude form. They are in "two's-complement" form. The value
-1 masked with
0x7F yields a value of
127, a far cry from the expected
1.
(The second reason is that
&& is a logical operator, giving a
true or
false result. The arithmetic and is represented by a single
&.)
The simplest syntax I know of for an absolute value function uses the ternary
if operator. It's cryptic, but effective:
Code:
(value<0?-value:value)