Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I know the difference between Signed and Unsigned but I was curious why they call them that? I did google for the answer but the response I find explain what the difference are and not what the terminology is so I thought I would ask real quick instead?

My guess is unsigned, being only positive numbers, means there is no minus SIGN in front of it but that is only guess.

Just wondering?
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Unsigned integers don't store any information about the sign of the number.

And the sign being the minus sign? So it just stores the numbers, that's it and that is why it is called unsigned and signed because of that minus sign.

So it is what I thought. Thanks for confirming it.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
My guess is unsigned, being only positive numbers, means there is no minus SIGN in front of it but that is only guess.

Just wondering?

The most significant bit in a signed integer indicates if the number is negative or not, this is the sign bit. You can verify this by trying to assign (1 << 31) to a signed integer which will set the bit and result in a negative number. A signed integer have the same range but half of it is used for negative values. For more information look up two's complement to see how they are represented on modern hardware.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
If a number is signed, the first bit is the sign bit. A sign bit of 0 corresponds to a + sign and a sign bit of 1 corresponds to a - sign.

With unsigned numbers, there's no bit that holds the sign information.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
39,782
7,514
Los Angeles
Here's an example, which assumes (since it's always true for Mac programming) that we're using the two's complement method for storing signed numbers. We're pretend we have only 4 bits in our integers:

How the 16 possible unsigned bit patterns are interpreted:
0000 means 0
0001 means 1 (which is the same as +1)
0010 means 2 (which is the same as +2)
0011 means 3 (which is the same as +3)
0100 means 4 (which is the same as +4)
0101 means 5 (which is the same as +5)
0110 means 6 (which is the same as +6)
0111 means 7 (which is the same as +7)
1000 means 8 (which is the same as +8)
1001 means 9 (which is the same as +9)
1010 means 10 (which is the same as +10)
1011 means 11 (which is the same as +11)
1100 means 12 (which is the same as +12)
1101 means 13 (which is the same as +13)
1110 means 14 (which is the same as +14)
1111 means 15 (which is the same as +15)

Conclusion: We can represent 16 different integers but all of them are positive.

How the 16 possible signed bit patterns are interpreted:
0000 means 0
0001 means 1 (which is the same as +1)
0010 means 2 (which is the same as +2)
0011 means 3 (which is the same as +3)
0100 means 4 (which is the same as +4)
0101 means 5 (which is the same as +5)
0110 means 6 (which is the same as +6)
0111 means 7 (which is the same as +7)
1000 means -8
1001 means -7
1010 means -6
1011 means -5
1100 means -4
1101 means -3
1110 means -2
1111 means -1

Conclusion: We can represent 8 different non-negative integers and 8 different negative integers.​
Notice that the unsigned bit patterns don't really represent unsigned numbers. Each bit pattern in both sets represents an integer, and integers by nature always have a sign. People tend to gloss over the distinction.
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Here's an example, which assumes (since it's always true for Mac programming) that we're using the two's complement method for storing signed numbers. We're pretend we have only 4 bits in our integers:

How the 16 possible unsigned bit patterns are interpreted:
0000 means 0
0001 means 1 (which is the same as +1)
0010 means 2 (which is the same as +2)
0011 means 3 (which is the same as +3)
0100 means 4 (which is the same as +4)
0101 means 5 (which is the same as +5)
0110 means 6 (which is the same as +6)
0111 means 7 (which is the same as +7)
1000 means 8 (which is the same as +8)
1001 means 9 (which is the same as +9)
1010 means 10 (which is the same as +10)
1011 means 11 (which is the same as +11)
1100 means 12 (which is the same as +12)
1101 means 13 (which is the same as +13)
1110 means 14 (which is the same as +14)
1111 means 15 (which is the same as +15)

Conclusion: We can represent 16 different integers but all of them are positive.

How the 16 possible signed bit patterns are interpreted:
0000 means 0
0001 means 1 (which is the same as +1)
0010 means 2 (which is the same as +2)
0011 means 3 (which is the same as +3)
0100 means 4 (which is the same as +4)
0101 means 5 (which is the same as +5)
0110 means 6 (which is the same as +6)
0111 means 7 (which is the same as +7)
1000 means -8
1001 means -7
1010 means -6
1011 means -5
1100 means -4
1101 means -3
1110 means -2
1111 means -1

Conclusion: We can represent 8 different non-negative integers and 8 different negative integers.​
Notice that the unsigned bit patterns don't really represent unsigned numbers. Each bit pattern in both sets represents an integer, and integers by nature always have a sign. People tend to gloss over the distinction.

There was more involved then I thought. This makes it totally clear what is happening.

Thanks guys. I can remember my teacher covering that in my Pascal class a couple years back.
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
The simple mathematical logic of signed numbers is:
if a + b = 0
then a = -b
and
b = -a​
If you assign a negative number to an int, then assign the value of that int to an unsigned int, you end up with an integer value that is very large, because all the assignment does is copy the bits over to the other field. If you then reverse the assignment, you get the negative number back. If you assign a negative number to a SInt64 (signed 64 bit value), then assign that to a UInt32 (unsigned 32 bit value), then reverse the assignment, the 64 will have the same value as the unsigned 32. This could be important to know in certain circumstances, but most of the time it will not affect program behavior (because most of the time, you will not do stuff like this).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.