+ operators

Jonathan M Davis jmdavisProg at gmx.com
Sat Jun 11 15:32:59 PDT 2011


On 2011-06-11 14:54, Renoir wrote:
> Sorry for the question but i'm an absolutely noob
> I have:
> 
> byte x = 10;
> byte y = 3;
> x = x + y;
> 
> why compilers complains?
> 
> Error: cannot implicitly convert expression (cast(int)x + cast(int)
> y
> ) of type int to byte
> 
> Have i to make another cast to sum byte + byte?

All integral operations where the types are int or smaller result in an int 
(unless you're dealing with unsigned types, in which case, I believe that the 
result would be uint). So, in this case the result of x + y is int. So, if you 
want the result to be byte, you have to do

x = cast(byte)(x + y);

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list