Author: Ricardo Gibert
Date: 04:58:17 09/28/03
Go up one level in this thread
On September 27, 2003 at 12:20:18, Daniel Clausen wrote:
>On September 27, 2003 at 08:22:47, Omid David Tabibi wrote:
>
>[snip]
>
>>In many cases, the data stored in integer variables determines
>>whether a signed or an unsigned integer type is appropriate.
>>For example, to record the weight of a person in pounds, no
>>negative numbers are required, so an unsigned type is
>>appropriate. However, recording temperatures in degrees
>>Celsius may require both positive and negative numbers, so a
>>signed type is needed.
>>
>>Where there is a choice of using either a signed or an unsigned
>>type, take into consideration that certain operations are faster
>>with unsigned types while others are faster for signed types.
>
>It probably won't surprise anyone, that my sole reason to use signed or unsigned
>is the fact whether the thing I want to represent _is_ signed or unsigned in
>nature.
>
>There can be a few problems when using 'subsets' of integers. (which unsigned
>can be considered, although the range on the positive scale is twice as big
>compared to signed int) The problem arises with loops. Consider this example:
>
>for(unsigned int i=63; i>=0; i--) { ... }
Both
for(unsigned i=63; (int) i>=0; i--) { ... }
and
for(unsigned i=63; i!=UINT_MAX; i--) { ... }
work using unsigned. The one that is troublesome is when you want to cover the
*entire* range of [un-]signed numbers e.g.
for(unsigned i=UINT_MAX; i>=0; i--) { ... }
when the type of snippet you give below will actually become necessary, since
changing "unsigned i" to "int i" won't solve the problem.
>
>That won't work as intended since the criterion when to break out of the loop is
>out of the range of unsigned. So this example would have to be re-written
>slightly:
>
>for(unsigned int i=63; i<=0;)
>{
> // do whatever
> if(i==0) break;
>}
>
>Not very elegant. In these cases I prefer to use the signed version.
>
>Sargon
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.