Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: a problem in trying to use unsigned varaibles

Author: Dieter Buerssner

Date: 14:53:20 09/24/03

Go up one level in this thread


On September 24, 2003 at 07:43:33, Uri Blass wrote:

>I was told that unsigned varaibles are better to use in every case that I can do
>it.

Perhaps, you misunderstood. I think, one should use unsigned, for things, where
one is likely to do some bit-operations. For example some flags variable:

if (flags & THIS_CONDITION)

Or more important shifts:

   some_condition = x >> some_shift;

Here using a signed integral type can easily yield in an error. Hash-keys,
bitboards, etc. all seem to call for an unsigned type.

>one of the varaibles that cannot get negative value is the ply of the search but
>the problem is that this varaible is connected with beta because I tell movei
>always that beta cannot be more than 9999-ply

I think, a signed integral type is a natural choice for ply.

>How do you solve that problem?
>
>practically I was too lazy to replace all my varaibles and decided only to
>replace few varaibles and remember that rule for new varaibles.
>
>It was another varaible that I defined as unsigned and was connected with ply
>and when I got the warning
>signed/unsigned mismatch I tried to change ply to be unsigned and got more
>warning of the same kind because beta is unsigned.
>
>I can get rid of the warning by adding signed.
>
>if (beta>9999-(signed)ply)
>		beta=9999-(signed)ply;

I suggest, to not use the cast. Such casts can easily hide errors, while the
code without the cast is fine.

Perhaps not in this particular case, but assume something like:

int foo(int a, unsigned int b)
{
  return a - b; /* just as an example */
}

No, assume you changed your program, and you recognized, that you need wider
types for the return value and for b (a is still fine as int). Say you want

INT64 foo(int a, UINT64 b)
{
  return a - b; /* will still work */
}

With the cast:
INT64 foo(int a, UINT64 b)
{
  return a - (signed)b; /* Oops, no warning, but probably wrong */
}

>I doubt if it is a good idea and it is possible that the conversion is going to
>cost time for the computer.

In practice, extremely unlikely (on 2 complemt's computers).

Regards,
Dieter





This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.