+ operators
Timon Gehr
timon.gehr at gmx.ch
Sat Jun 11 15:45:42 PDT 2011
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?
Yes, the compiler casts all operands to 'int' when performing arithmetics. You can
explicitly cast it back, or you can do:
x = (x+y) & 0xff;
shorter, safer and nicer in general.
If you compile with -O, this won't even have an overhead.
Timon
More information about the Digitalmars-d-learn
mailing list