signed or unsigned ???

Started by
10 comments, last by silvermace 21 years, 8 months ago
LOL! Remind me never to make a definitive statement again. Anyway, I checked the "AMD Athlon x86 Code Optimization Guide" and this is its general recommendation for C-level optimisation:

Use unsigned types for:
* Division and remainders
* Loop counters
* Array indexing

Use signed types for:
* Integer-to-float conversion

(p19, chapter 3.)

Things it says (p18, chapter 3):
quote: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.

Integer-to-floating-point conversion using integers larger than 16 bits is faster with signed types


Not that it matters much, I guess. I tend to use unsigned ints for most loops, counts and sizes as they usually make more sense as non-negative.

I don''t see this is an especially religious issue (which is why I shouldn''t have made a definitive statement). Use whatever data type makes more sense in the situation (or take the other advice that it should always be signed).
Advertisement
I agree that unsigned make more sense for array sizes \ loops, however I''ve had some difficult to find errors in my logic where I was attempting to access element -1 (Normally when looping through an array backwards and miscounting where to start \ stop). In this case it took me a while to locate the code as it ''looked'' correct. The reason here is that the unsigned int was underflowing(term?) after it hit zero and an array element of size::unsigned int didn''t exist

Sometimes using signed anyway is better for debugging. That said, I don''t follow my own advice and use unsigned anyway

Chris Brodie
http:\\fourth.flipcode.com
Chris Brodie

This topic is closed to new replies.

Advertisement