Free Consulting
Free Consulting

Reputation: 4402

What exactly being computed in this expression, and why?

#define NP_MAXREADY (((unsigned)(~0)<<1)>>1)

I take it as: fill register sized unsigned int with ones, then shake off MSB, obtaining maximum value for signed int. Is it correct? Also, the reason why they are doing it such way completely evades me, please enlighten.

Upvotes: 4

Views: 133

Answers (2)

Jens Gustedt
Jens Gustedt

Reputation: 78903

You may rewrite this as

#define NP_MAXREADY (((~0u)<<1)>>1)

then you'd notice that the inner shift operation is completely useless, since its only effect is to shift out the highest order bit

#define NP_MAXREADY ((~0u)>>1)

which in turn is nothing than

#define NP_MAXREADY (UINT_MAX/2)

Other than stated in another answer, this is not INT_MAX, since first of all this one here is an unsigned, so the type is different. Then the representation of signed versus unsigned may have padding bits, so you never can be sure that these two have the same value.

Upvotes: 7

We'd have to see the code that uses it to be sure, but "max value of a signed integer" would also be my guess.

As for why they are doing it this way - probably because they don't know about INT_MAX

Upvotes: 7

Related Questions