Silly Signed / Unsigned question

Discussion in 'Mac Programming' started by larswik, Jan 4, 2013.

  1. macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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?
     
  2. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #2
    Unsigned integers don't store any information about the sign of the number.
     
  3. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    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.
     
  4. macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    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.
     
  5. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #5
    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.
     
  6. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #6
    Sign being a property of a number, either positive or negative.
     
  7. Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #7
    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.
     
  8. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    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.
     
  9. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    #9
    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).
     

Share This Page