Skip to main content
GameDev.net gamedev.net
🔒 Locked

why signed and unsigned

Started by silverphyre673 Apr 11, 2005 at 1:19 AM 25 replies 5k views
Original Post
silverphyre673
silverphyre673
What is the significance of the names 'signed' and 'unsigned'? (I know what they do). i.e why would they have chosen 'signed' to mean that a number can be both positive and negative? Also, can anyone think of any easier to understand words? Its for a small scripting language I'm making. Thanks.
my siteGenius is 1% inspiration and 99% perspiration
iMalc
iMalc
It makes perfect sense to me, and I can't think of anything else shorter that accurately describes signed numbers. Sorry that probably doesn't help...
silverphyre673
silverphyre673
they make sense to me, too, but I don't know what exactly it means for a number to be signed. You sign your name, not numbers :)
my siteGenius is 1% inspiration and 99% perspiration
Sneftel
Sneftel
The 'sign' that is referred to is the negative/positive sign.
cignox1
cignox1
I think signed refers to the '-' and '+' signs that must be added ('+' is omitted, but conceptually it is there) to fully rapresent the number. Unsigned says that there are no signs in the head.
Drew_Benton
Drew_Benton
[edit] Whoa this filled up fast

Quote:
Original post by silverphyre673
What is the significance of the names 'signed' and 'unsigned'? (I know what they do). i.e why would they have chosen 'signed' to mean that a number can be both positive and negative? Also, can anyone think of any easier to understand words? Its for a small scripting language I'm making. Thanks.


Because if something is 'signed' it can either have a + sign or a - sign, everytime you write some constant number, and it does not have a minus sign on it, it really is interpreted as + that number, ie +4, +6, +9.

However, we base our numbering system to where if it doesn't have a minus, assume positive. I think that signed is a good fit for what it is refering too. I mean if you wanted something that could be either or, the only thing I could think of is 'neutral' for unsigned, but then that gets into what does it mean?

Best bet is to go with standards, if you use signed/unsigned, most programmers will know right away what it means, but it is your scripting language [wink] Good luck!
kzar
kzar
Well the last time I used an unsigned thing, I needed an unsigned char because ascii goes from 0 to 200+ and with a normal char high numbers came out negative and messed up my program.
Nice Coder
Nice Coder
Yes. Its over 126, so it went negitive.

[google]

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
sjelkjd
sjelkjd
Unsigned chars (usually*) in C and C++ represent numbers in the range [0,255], and signed chars do [-128, 127].

*although not guaranteed by the standard
silverphyre673
silverphyre673
OK! I think that... actually wow! holy crap! How did this generate so many responses so quickly? I get it, OK? I get it! You don't all have to yell at me!

Geez.
my siteGenius is 1% inspiration and 99% perspiration
ju2wheels
ju2wheels
wow, im very surprised that most people missed the real importance of signed/unsigned numbers, THE NUMBER RANGE!!!!

You can represent a larger range of strictly positive numbers with signed variables.

When you make the variables unsigned, that range is split in half, with half the range being negative values and the other half positive.

If you go past the range in either direction, then it wraps back around and starts counting from the opposite end of the range (depending on what you are doing with the variable)...

See sjelkjd's post above on ranges.... the same amount of charactersa are represented in both ranges, but signed or unsigned changes where the range starts and ends.
bakery2k1
bakery2k1
Quote:
Original post by ju2wheels
You can represent a larger range of strictly positive numbers with signed variables.

When you make the variables unsigned, that range is split in half, with half the range being negative values and the other half positive.


Other way round. [smile]
nmi
nmi
The binary representation of signed and unsigned numbers in C/C++ is identical, for negative numbers you have to take the 2s-complement of that number.

The difference between signed and unsigned numbers is when you shift it to the right. Signed numbers are sign extended (0 for positive and 1 for negative) numbers, wheras unsigned numbers are always 0 extended. Another difference happens when comparing two numbers, since a signed negative number may be smaller even if its 2s-complement is greater.

Daaark
Daaark
Because signed / unsigned toggles the sign bit.
nmi
nmi
Quote:
Original post by Vampyre_Dark
Because signed / unsigned toggles the sign bit.


For floating point numbers, there is a sign bit, which can be toggled.

For integer numbers, and signed/unsigned are such, there is no such bit.
Instead the 2s-complement of a number is stored.

For instance

signed char num = -3;

is the same as

unsigned char num = 253;

The stored byte is 0xfd in both cases.
Daaark
Daaark
Quote:
The left most bit of a signed integer is called the "sign bit".
Quote:
If bm-1 is 1 then the value is negative; otherwise it is non-negative. Thus the high-order bit, bm-1 is called the sign bit.

I verified it before I posted.
antareus
antareus
I would make it so the user can't specify signedness. It is a low-level detail best left to lower-level languages.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Agony
Agony
Quote:
Original post by Vampyre_Dark
Quote:
The left most bit of a signed integer is called the "sign bit".
Quote:
If bm-1 is 1 then the value is negative; otherwise it is non-negative. Thus the high-order bit, bm-1 is called the sign bit.

I verified it before I posted.
While there is a sign bit, saying that "signed / unsigned toggles the sign bit" is potentially misleading, as it is more complex than merely toggling a single bit, like nmi explained.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
Fruny
Fruny
4-bit unsigned:
n = b0.20+b1.21+b2.22+b3.23

4-bit signed:
n = b0.20+b1.21+b2.22-b3.23

Extend to any number of bits.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
rpg_code_master
rpg_code_master
Signed and Unsigned are simple.

Each data type is a certian number of bits long.

Eg, the type integer is 8 bits long (on most systems). So this enables an integer to stores 65536 different numbers.

A signed data type can only store positive numbers. So an 8 bit signed integer can store the numbers 0 to 65535

An unsigned data type on the other hand can store both positive and negative numbers. So to give the most flexibily, an unsigned data type stores the same amount of negative numbers as it does positive numbers. So an 8 bit unsigned integer can store the numbers -32767 to 32768. (Note, the 0 also counts as a number ie, 00000000)

I hope this helps.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.