value range propagation for _bitwise_ OR

"Jérôme M. Berger" jeberger at free.fr
Tue Apr 13 10:40:52 PDT 2010


Fawzi Mohamed wrote:
> 
> On 13-apr-10, at 12:02, Don wrote:
>> It's been part of DMD2 for a while now. It allows you to do things like:
>>
>> ubyte lowbits(int x)
>> {
>>    return x & 7;
>> }
>>
>> without an explicit cast. The compiler knows that x&7 can safely fit
>> inside a single byte.  Whereas   ((x&7) << 12) | (x&3);
>> does not fit, and requires an explicit cast.
> 
> ah ok I understand, I thought that was treated like x & cast(ubyte)7 ,
> and so as comparison of the compile time value with the ranges of ubyte
> (no range propagation needed).
> But I can understand why it is treated as cast(ubyte)((cast(int)x) & 7),
> as it is probably easier for the compiler, as it upcasts by default.
> 
> Thanks for the explanation.
> 
	Note that in Don's example, "x" is an int, not a ubyte, so you
don't need the cast to "int" in your second example, but you still
need range propagation in the first...

		Jerome
-- 
mailto:jeberger at free.fr
http://jeberger.free.fr
Jabber: jeberger at jabber.fr

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100413/5c12f591/attachment.pgp>


More information about the Digitalmars-d mailing list