Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly being computed in this expression, and why?

Tags:

c

#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.

like image 423
Free Consulting Avatar asked Dec 10 '22 10:12

Free Consulting


1 Answers

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.

like image 50
Jens Gustedt Avatar answered Dec 29 '22 21:12

Jens Gustedt