implicit ubyte casting
moritzwarning at web.de
Thu Oct 1 09:19:41 PDT 2009
On Thu, 01 Oct 2009 10:16:08 -0400, Jeremie Pelletier wrote:
> Saaa wrote:
>> I think is very bug-prone, isn't it obvious iub should be -5?
>> ubyte ub = 5;
>> int iub = -ub; // iub now is 251
>> What is the reasoning to do it this way?
> Minus toggles the most significant bit, be it on a signed or unsigned
> type. When converting it to an int, the byte being signed or unsigned
> does make a difference: when unsigned the number is copied as is, when
> signed the most significant bit (bit 7) is shifted to the most
> significant bit of the int (bit 31).
> Its therefore pretty standard logic, no warning is given since the
> entire ubyte range fits within an int
This is a troublesome behavior:
ubyte z = 5;
int x = -z; // x now is 251
int y = -1 * z; // y is now -5
More information about the Digitalmars-d-learn