Why isn't the overflow flag set?

Timon Gehr timon.gehr at gmx.ch
Thu Nov 10 11:28:30 PST 2011


On 11/10/2011 06:24 PM, Simen Kjærås wrote:
> I'm creating a Checked!T struct for integral values. The version
> attached works
> for the most part, but some parts confuse me, namely that the following
> operations do not set the overflow flag:
>
> uint.max + 1
> ulong.max + 1
>
> int.min - 1
> uint.min - 1
> long.min - 1
> ulong.min - 1
>
> uint.max << 1
> long.max << 1
> ulong.max << 1
>
> int.min << 1
> long.min << 1
>
> int a = int.max; a *= 2
> long a = long.max; a *= 2
>
> The following feel wrong to me, but I guess there may be reasons behind
> them:
>
> ubyte a = ubyte.min; --a
> ushort a = ushort.min; --a
> uint a = uint.min; --a
> ulong a = ulong.min; --a
>
> ubyte a = ubyte.max; ++a
> ushort a = ushort.max; ++a
> uint a = uint.max; ++a
> ulong a = ulong.max; ++a
>
> ubyte a = ubyte.max; a *= 2
> ushort a = ushort.max; a *= 2
> uint a = uint.max; a *= 2
> ulong a = ulong.max; a *= 2
>
> ubyte a = ubyte.min; a -= 1
> ushort a = ushort.min; a -= 1
> uint a = uint.min; a -= 1
> ulong a = ulong.min; a -= 1
>
> ubyte a = ubyte.max; a <<= 1
> ushort a = ushort.max; a <<= 1
> uint a = uint.max; a <<= 1
> ulong a = ulong.max; a <<= 1
>
> Are some of these supposed to work but don't?

The overflow flag indicates that a signed overflow has occurred. To test 
for unsigned overflow, check the carry flag with jc.



More information about the Digitalmars-d-learn mailing list