Silly Signed / Unsigned question

macrumors 68000

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.

macrumors 68040

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?

Click to expand...

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.

Administrator

Staff Member

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.

thread startermacrumors 68000

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.

Click to expand...

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.

macrumors 68000

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).

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.