Is unary minus equivalent to binop minus?

My C compiler gave a warning when using a unary minus in the value unsigned

, so I fixed that warning by doing a subtraction from 0.

Now I'm wondering if the current code matches the original one:

uint32_t a, b; // assume b is initialized and non-zero

a =  -b   % b; // old code
a = (0-b) % b; // current code

      

My question is, for the same values, will b

both lines of code get the same result for a

?

+3


source to share


1 answer


Usually, yes, if you uint32_t

have a narrow type on your platform . Then it will first be promoted to int

, and negation will be done in that type.



+3


source







All Articles