Operator Precedence in C

This blog record how I debugged a problem while I was implementing float subtraction solely based on integer subtraction.

Operator Precedence in C

What is some potential problem of the following code:

This code is a special case when subtracting two float numbers with different sign.

1
2
3
4
5
6
7
8
if (sign_f != sign_g) {
sign_res = sign_f;
mantissa_res = mantissa_f + mantissa_g;
if (mantissa_res & BIT24_MASK == BIT24_MASK) {
mantissa_res >>= 1;
exp_res++;
}
}

As I mentioned in the title, I was trying to check whether mantissa_f + mantissa_g exceeded the 2. If so, I need to reduce it to 1 point something and change the exponent. Yet the if statement does not work as I expected. This is because the == operator has a higher precedence than the & operator.

Thus, the correct way of writing is if ((mantissa_res & BIT24_MASK) == BIT24_MASK) or if (mantissa_res & BIT24_MASK)


Operator Precedence in C
http://blog.slray.com/2023/10/26/Operator-Precedence-in-C/
Author
Sirui Ray Li
Posted on
October 26, 2023
Licensed under