Search code examples
cunsignedunsigned-integeransi-c

Modify line of code in c to NOT contain unsigned variable or casting. (?)


How could I modify the line bellow in ANSI C without using the unsigned mark?

unsigned int x, y, z; // unsigned variables should not be used
/*... some operations where x, y and z gets values between 0x0 and 0xFFFFFFFF ... */
x = (unsigned int)-(int)(y * z); // line to modify

Solution

  • You can directly apply negation to an unsigned value. The result will be reduced modulo 2^N, which is what your existing code does anyway.

    x = -(y * z);