[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lacking an unsigned MAX macro
- From: Francisco Olarte <folarte@...>
- Date: Wed, 15 May 2019 11:39:49 +0200
Philippe:
On Wed, May 15, 2019 at 4:33 AM Philippe Verdy <verdy_p@wanadoo.fr> wrote:
> In C, 0 is an expression of type int; writing ~0 creates an unary expression taking an int: the ~ operator only uses the standard type promotion (to int) by default and no other longer type (if you pass a short, that short will be promoted to an int), then it computes the bit-inversion (i.e. its a simple arithmetic addition: substract the value from the maximum of its input type and adds the minimum of that type); as this input type is an int, , it returns an int equal to (INTMAX - 0 + INTMIN). This gives the value you use in the initializer of your declared variable: It's only at that time that there will be a conversion from int to lua_Unsigned ! This conversion being possibly lossy if the target type cannot contain the full range for you initializer value (and because you did not provide an, explicit typecast to the result, the compiler should warn you.
You've got a weird concept of simple. Why go through this arithmetic
addition stuff ( which needs, among other things, a second operand ),
when the programmer requested a bitwise negation and many instruction
sets have a perfectly good unary negation instruction? ( and others
have similar logic operations which are easier for logic stuff ).
Francisco Olarte.