using the full range of ubyte with iota

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 25 10:59:04 PST 2015


On Sun, 25 Jan 2015 14:11:09 +0000, Dominikus Dittes Scherkl wrote:

> On Sunday, 25 January 2015 at 13:03:16 UTC, bearophile wrote:
>> Dominikus Dittes Scherkl:
>>
>>> Because this is useful in more situations,
>>
>> Right, but it's still a cast. And in D you want to minimize the number
>> of usages of casts. The proposed syntax iota!"[]" is cast-safe.
>>
> I don't case too much, if I have ensured the cast is safe by constraints
> beforehand.
> 
> I need to cast often anyway, because I work with small types and most
> operators permanently change everything to "int", especially the
> bit-operations for which a signed type makes no sense at all:
> 
> ubyte x = 50;
> auto y = x & 0x11; // y is int! I hate that!
> 
> even if I use unsigned literals:
> auto z = x & 0x12u; // z is uint - better but still bad. More so as &
> should result in the smaller of the two types!!
> 
> But I need not even use literals (which unfortunately cannot be makred
> as "ubyte" or "short"). Look at this:
> 
> auto x2 = (x>>4) | (x<<4); // swap nibbles - but result in an int!!!!!

this is true for C and C++ too, as all three languages doing "integer 
promotion". the only difference is that D forbids potentially lossy 
assigns.

you best bet is to not use `auto`, but specify required type explicitly. 
or use ints/uints and cast to bytes only when it is necessary.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150125/2e02954c/attachment.sig>


More information about the Digitalmars-d-learn mailing list