signed or unsigned ???

Started by
10 comments, last by silvermace 21 years, 9 months ago
whats the diff between a signed and an unsigned variable, what advantages/disadvanteges, uses and when should i use them ?? thanx, silvermace
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Advertisement
The differences are max values and what they can store, basically. A signed number lets you store negative values - the sign representing positive or negative. Therefore, if you wanted the value '-30' you'd want a signed value. Unsigned values only allow positive values.

Signed numbers store their sign by using a bit of the variable to store positive or negative - if the bit is 1 then the value is negative, otherwise it's positive. This means that they will always have a smaller max value than their unsigned equivalent as they're not using the entire variable to store the value. For example, a signed 16-bit variable has the max value 2^15 but an unsigned variable has 2^16 (as it doesn't have to use a bit to say whether the number is negative).

EDIT: Just so it's clear - both can store the same range of values (for 16 bits = 65536), it's just the the maximum values are shifted. The signed number goes from –32768 to 32767 and the unsigned goes from 0 to 65536. Both ranges are the same, but the maximum value for the unsigned value is larger (65536 versus 32767). (This is the same for any data type, I just couldn't be bothered writing out the example for 32 bits ).

Use unsigned numbers where possible. They're quicker for the CPU to deal with and make more sense in some uses (for example, to store sizes and counts). The maximum value is higher too, which may be relevant in some cases. The only two times you'll want to use signed integers is when you expect a negative value and when you want to avoid converting between signed and unsigned values (which may require extending the result to a larger type, which will be slow).

[edited by - Alimonster on July 29, 2002 6:42:28 AM]
cheers mate. that help alot !
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
... and using unsigned integers prevents you checking for things like negative array indicies and memory allocation sizes.....

Couple of things to add here:
Signed and unsigned variables are treated completely differently when bit-shifting right. Signed variables will sign-extend the high bit: it will pad the high bits with zeros if the original high bit was zero, and it will pad the high bits with ones if the original high bit was one. If your variable is unsigned, you''ll always get zeros padded when right-shifting.

Also, I''ve heard it said that if you switch a number from signed to unsigned to "gain one bit" in magnitude, you should seriously think about just bumping it up to the next largest storage type; it''s an indication that you''re close to overflowing and have under-estimated the size of that spot. Choosing unsigned often has unpleasant effects as far as conversion of pointers, etc., so unless it''s absolutely necessary (e.g. sign extension when shifting), you''re probably best of leaving everything its default, which is signed.

(BTW, this last paragraph of advice comes from Lakos''s "Large-Scale C++ Design"; I''m a little more than half-bought-into this argument, but not 100%, so take it with a grain of salt).
quote:Original post by Alimonster
Signed numbers store their sign by using a bit of the variable to store positive or negative - if the bit is 1 then the value is negative, otherwise it's positive.

Yep, it 'almost' work this way. Why I say almost? See here:
If you say 'that' is the sign bit, then:  32767 = 0111 1111  1111 1111 -32767 = 1111 1111  1111 1111what about -32768?? It doesn't make sense to have -0 as -32768 if you says 'that' is the sign bit. -------------------------------------------It should be this way, eg. short    /_int16    :          1: 0000 0000  0000 0001        ...      32767: 0111 1111  1111 1111     -32768: 1000 0000  0000 0000     -32767: 1000 0000  0000 0001        ...         -1: 1111 1111  1111 1111--------------------------------------------why not -1 = 1000 0000  0000 0001 ?why not -0 = 1000 0000  0000 0000 (heheheh... )   

Just search for 2's complement... then you should know.





[edited by - DerekSaw on July 29, 2002 9:26:15 PM]

[edited by - DerekSaw on July 29, 2002 9:27:16 PM]
"after many years of singularity, i'm still searching on the event horizon"
quote:Original post by DerekSaw
Just search for 2''s complement... then you should know.

Way ahead of you, dude.
quote:Original post by Alimonster
Way ahead of you, dude.

That''s good. I just wanna make sure silvermace knows what''s behind.

"Assumption is the mother of all fuck ups." - Under Siege 2
"after many years of singularity, i'm still searching on the event horizon"
Unsigned variables are good for doing binary level manipulation I find. I often wish VB had unsigned types other than a Byte because of this.

Trying is the first step towards failure.
Trying is the first step towards failure.
quote:
Use unsigned numbers where possible. They''re quicker for the CPU to deal with and make more sense in some uses (for example, to store sizes and counts).


There is no speed difference when it comes to signed versus unsigned integers. This is why I wish more people knew assembly language better.

The CPU doesn''t really make a distinction between signed and unsigned in the way that most programming languages do. When making comparisons, it''s just a matter of using different flags (those which assume the operands are signed vs. those which are for unsigned operands.) There are usually different shift right instructions (SHR for unsigned, SAR for signed) and MUL/DIV instructions (MUL, IMUL, DIV, IDIV.)

The same instructions are used for most operations, so it''s no big deal which you choose in terms of speed.


---
Bart
----Bart

This topic is closed to new replies.

Advertisement