Quote:
|
Originally Posted by psquared
could you explain. We are not trying to make p1_x greater than 255, we are just trying to use it to make an int greater than 255...
value = (2)*(p1_x)+256;
I would think that is ok, but if not, how could we correct it to get the same result.
thanks.
|
Sorry, I read your printf and it didn't match the order of your example.
try typecasting
value = 2*(int) p1_x + 256;
[edit] What's happening is the operation on the right will use the size of the largest type in the equation to store the result temporarily. In this case an unsigned char (p1_x).
The 2* calculation overflows the unsigned char temporary storage.
Adding the (int) forces the compiler to use the larger integer type to temporarily store the result.