> uint a = 10;
> uint b = 11;
> uint c = a - b; // whoops
>
> You must switch c to a signed int, to get the right result. In this
> example its easy to see, but IRL coding the above might not be that
I don't get it. On my system, both int and uint produce c == 0xffffffff.
There are no separate instructions for signed/unsigned addition and
subtraction. The binary rep of signed numbers is "two's complement",
so it just wraps around -- raising a "carry" flag on the cpu if it
does so. If the code in question is in a checked block, *then* it'll
throw:
checked {
uint a = 10;
uint b = 11;
uint c = a - b; // whoops
}
IMHO, the more common mistake is comparisons of signed- and unsigned
values. I wish they'd add a compiler warning for some of these
easy-to-spot cases...
uint a = 10;
uint b = 11;
uint c = a - b; // ok, =0xffffffff
if (c < 0) { // oops, never true!
Cheers,
-Shawn
http://msdn.com/tabletpc
http://windojitsu.com
On Mon, 22 Nov 2004 12:53:47 +0100, Ryan Heath <[EMAIL PROTECTED]> wrote:
> Overloading methods based on signed/unsigned alone
> is adding confusion instead of convenience ...
>
> Having a broader type (Int64) where applies, sounds very good,
> although dont know whether VB.net is supporting it "out-of-the-box".
>
> Can you "publish" a next version? that uses signed types (and Int64
> where applies) in the public methods?
>
> // Ryan
>
> PS
> At first, unsigned types seems a very good approach, until you start
> doing math with them.
>
> Look at this simple example.
>
> uint a = 10;
> uint b = 11;
> uint c = a - b; // whoops
>
> You must switch c to a signed int, to get the right result. In this
> example its easy to see, but IRL coding the above might not be that
> obvious...
>
> ===================================
> This list is hosted by DevelopMentor� http://www.develop.com
>
>
> Some .NET courses you may be interested in:
>
> Essential .NET: building applications and components with C#
> November 29 - December 3, in Los Angeles
> http://www.develop.com/courses/edotnet
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>
===================================
This list is hosted by DevelopMentor� http://www.develop.com
Some .NET courses you may be interested in:
Essential .NET: building applications and components with C#
November 29 - December 3, in Los Angeles
http://www.develop.com/courses/edotnet
View archives and manage your subscription(s) at http://discuss.develop.com