+ operators

Jonathan M Davis jmdavisProg at gmx.com
Sat Jun 11 17:48:13 PDT 2011


On 2011-06-11 16:43, renoir wrote:
> Ok.
> 
> So i can do:
> 
> x = cast(byte)(x + y);
> x = (x + y) & 0xff;
> 
> is there any difference in terms of performance?

That would depend entirely on the optimizer. I believe that the cast is 
guaranteed to cost nothing. The & might cost something if you don't compile 
with -O, but even then cost is very small. Ultimately, they're probably the 
same, but it depends on what exactly the compiler does. Personally, I much 
prefer the cast because it's more immediately clear to me what you're doing. 
But if someone uses the & 0xff trick all the time, then that's probably quite 
clear to them as well.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list