Question about data types

I'm not sure if this is about data types, but it's in the same example code as I referred to above. I ran my code and compared it to the example code (which I assume is correct :)), and got different results. After tracking it down I found that my code gave different results because I somewhere had a line " P = P + (x1+x2+3791) >> 4;", but when I changed it to "P += (x1+x2+3791) >> 4;", I got the same results as the example code. As I understand it the terms " X = X + Y;" is the same as "X += Y;", isn't that so?

Lars81: I'm not sure if this is about data types, but it's in the same example code as I referred to above. I ran my code and compared it to the example code (which I assume is correct :)), and got different results. After tracking it down I found that my code gave different results because I somewhere had a line " P = P + (x1+x2+3791) >> 4;", but when I changed it to "P += (x1+x2+3791) >> 4;", I got the same results as the example code. As I understand it the terms " X = X + Y;" is the same as "X += Y;", isn't that so?

X = X + Y and X += Y are the same when X and Y are atomic values

When Y is a sum then it makes a slight difference.

P = P + (x1+x2+3791) >> 4;

is not the same as

P += (x1+x2+3791) >> 4;

BUT

P = P + ((x1+x2+3791) >> 4);

is.

It's all about operator precedence.

  • has a higher priority than >>, so in the P = P + ... version the right shift happens on the sum of P + (x1+x2+3791). In the P += ... version the right shift happens on just the (x1+x2+3791) portion.