K
Kevin Easton
Christopher Benson-Manica said:Certainly would have been my first choice, but all conditional operators were
explicitly forbidden by the assignment. The 32-bit integer assumption was
explicitly permitted, and two's complement represenation was implied. Since
the code was only required to work on a specific implementation (an i386 Linux
box, I believe), these assumptions were acceptable in the context of the
assignment.
Is this question impossible to answer in a strictly ANSI-compliant way, then?
Not at all:
x * (2 * (x > 0) - 1)
(Chances are this will result in a branch in the generated machine code
- but that's not what the question asked).
- Kevin.