开发者

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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜