Casts and some suggestions to avoid them

bearophile bearophileHUGS at lycos.com
Tue Apr 8 14:21:52 PDT 2014


H. S. Teoh:

>> In some cases you can remove such casts using a union (like a 
>> union of one ulong and a uint[2]).
>
> Using a union here is not a good idea, because the results 
> depend on the
> endianness of the machine! It's better to just use (a & 0xFFFF) 
> or (a >> 16) instead.

Better to avoid magic constants, you can forget one F or 
something. In this case you have to use 0xFFFF_FFFFu. This is 
safer and more readable:

a & uint.max

Bye,
bearophile


More information about the Digitalmars-d mailing list