Binary numbers, signed, unsigned.

This is a discussion on Binary numbers, signed, unsigned. within the Tech Board forums, part of the Community Boards category; I am trying to read up on binary numbers but I do not fully understand how I can see i ...

  1. #1
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59

    Binary numbers, signed, unsigned.

    I am trying to read up on binary numbers but I do not fully understand how I can see i a binary number is signed or unsigned.

    Could someone explain this for me?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It all depends on how the signed number is represented in binary, common techniques include ones complement, two's complement, excess-k or signed magnitude.

    The easiest being signed magnitude, where the most significant bit is used to for the sign (i.e. 1 is negative, 0 is positive). eg,
    5 = 0101
    -5 = 1101

    [edit]
    See Signed number representations - Wikipedia, the free encyclopedia
    [/edit]
    Last edited by zacs7; 11-11-2009 at 05:39 AM. Reason: link

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    There is always a signed bit attached with the number it will depict that

  4. #4
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Does that mean that if a number is stored in 8-bits for example "01000001" the 7th bit (0 to 7) refers to that it is a positive number?

    Then how do you prevent mistakes? For example "10111111" is -65 in binary-2 but it also refers to the number 191 in decimal.

    Edited:

    Quote Originally Posted by RockyMarrone View Post
    There is always a signed bit attached with the number it will depict that
    Ok that explains more so by requiring a signed bit this mistake can not be made?

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by RockyMarrone View Post
    There is always a signed bit attached with the number it will depict that
    No there isn't. Most CPU's operate in unsigned magnitude, two's complement or excess-k ... usually.

    The CPU will know how the signed numbers are represented (it will infact require them to be a certain representation). Usually there are math instructions provided for both unsigned and signed integer math.

    Code:
    For example "10111111" is -65 in binary-2 but it also refers to the number 191 in decimal.
    Reading "10111111" as signed magnitude gives, (working from right to left)
    2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5
    = 63
    And the MSB is "1", which gives a negative sign.
    = -63

    Using signed magnitude again, if you wanted to represent 63 then that's "00111111". Note that the MSB (left most bit) is now binary 0.
    Last edited by zacs7; 11-11-2009 at 06:15 AM.

  6. #6
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Quote Originally Posted by zacs7 View Post
    Reading "10111111" as signed magnitude gives, (working from right to left)
    2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5
    = 63
    And the MSB is "1", which gives a negative sign.
    = -63

    Using signed magnitude again, if you wanted to represent 63 then that's "00111111". Note that the MSB (left most bit) is now binary 0.
    Weird my book tell me to reverse "01000001" and add 1 to it to create the negative number of 65 (according to the book "01000001" is 65).

    Maybe I am missing out on something here. Is there a difference to the decimal number 65 "01000001" and the binary one?

    Beside that I found this.
    In the C programming language, you can designate numbers as either signed integers or unsigned integers (they are signed by default if you do not specify otherwise).

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by JOZZY& Wakko View Post
    Weird my book tell me to reverse "01000001" and add 1 to it to create the negative number of 65 (according to the book "01000001" is 65).

    Maybe I am missing out on something here. Is there a difference to the decimal number 65 "01000001" and the binary one?

    Beside that I found this.
    That is two's complement representation -- see http://en.wikipedia.org/wiki/Two%27s...27s_complement . Not signed magnitude as discussed above.

    Your quote really has nothing to do with any of this, other than you cannot use bitwise operators on signed integers in C. And, unsigned integers are stored as unsigned magnitude in C -- as per the standard.
    Last edited by zacs7; 11-11-2009 at 06:21 AM.

  8. #8
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Quote Originally Posted by zacs7 View Post
    That is two's complement representation -- see Two's complement - Wikipedia, the free encyclopedia . Not signed magnitude as discussed above.

    Your quote really has nothing to do with any of this, other than you cannot use bitwise operators on signed integers in C. And, unsigned integers are stored as unsigned magnitude in C -- as per the standard.
    Thanks you for the link. This stuff kind of confuses me at this moment especially because it is not in my native language so I will try to find some more information on it in Dutch and come back on the subject if I do not understand something after that.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by JOZZY& Wakko View Post
    Thanks you for the link. This stuff kind of confuses me at this moment especially because it is not in my native language so I will try to find some more information on it in Dutch and come back on the subject if I do not understand something after that.
    Does this not explain it correctly?

    Two's complement - Wikipedia (Nederlands)
    Sent from my iPad®

  10. #10
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Quote Originally Posted by SlyMaelstrom View Post
    Does this not explain it correctly?

    Two's complement - Wikipedia (Nederlands)
    Yes it does but I am getting kind of confused with all the different systems mentioned to do calculations.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zacs7 View Post
    you cannot use bitwise operators on signed integers in C.
    Of course you can, just remember they are two's compliment numbers.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by MK27 View Post
    Of course you can, just remember they are two's compliment numbers.
    They don't have to be, that's implementation defined (See ISO 9899 Annex J Portability issues) -- discusses ISO 9899 6.2.6.2 Integer types. Even if the implementation specifies that the signed integers are two's complement. It must also specify what, and how the bitwise ops will work along with padding and signed bits. Even then, there is still some room for undefined behaviour.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    I think the point of the question was being missed here.

    There is no way of telling if a given binary representation is signed or unsigned.

    Quote Originally Posted by JOZZY& Wakko View Post
    Does that mean that if a number is stored in 8-bits for example "01000001" the 7th bit (0 to 7) refers to that it is a positive number?

    Then how do you prevent mistakes? For example "10111111" is -65 in binary-2 but it also refers to the number 191 in decimal.
    Exactly.
    Whether the number is 2s complement, or 1s, or whatever the others have written about above, is not the issue if I understand the question.

    The point is it's up to the interpretation that's used. The stored value could be an ASCII character, or some arbitrary flags - neither of which make sense if treated as an integer. All that's required is that the meaning of the data is agreed upon by both the writer/creator and the reader/user.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    It's funny you said it nonoob. I've followed the discussion and all the time I felt the same as you. I decided to not say anything because... well, the OP was answering the arguments and no mention was being made that the answers weren't addressing his actual question.

    ...

    To make it more clear (I think):

    Just by looking at a series of binary digits there's is no way you can tell what they mean. Binary representation lacks structure and meaning. It's just a bunch of ones and zeros. The actual structure and meaning is given by providing those 1s and 0s with some sort of structure and a meaning.

    From nonoobs quote:

    01000001 has no meaning whatsoever. It gains a meaning when you read it within a given context. For example

    A byte representing an 'ascii' char: 01000001 = 'A'
    An integer: 01000001 = 65

    Conversely:
    10111111 can mean a few different things:

    If read into a signed byte: -65
    if read into a unsigned int: 191
    Last edited by Mario F.; 11-19-2009 at 09:49 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Thanks I was already getting pretty lost on that one until I found a good explanation yesterday in "C by example". Up to that moment I just thought I was going crazy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Char Help! "Packing " bits to a signle unsigned char
    By xxrexdartxx in forum C Programming
    Replies: 7
    Last Post: 10-11-2009, 05:45 AM
  2. Replies: 8
    Last Post: 06-04-2009, 03:03 PM
  3. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. - signed int to binary?
    By KnowledgeHungry in forum C++ Programming
    Replies: 7
    Last Post: 08-25-2003, 12:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21