#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    30
    Rep Power
    1

    Converting Decimal to Two's-Complement


    Instructions: Convert these decimal numbers to 5-bit 2's-complement form, if possible. If not possible, explain why this is so.

    (16) base 10

    According to the online converter:
    Code:
    http://s7.postimg.org/sfyejx8yz/error.png
    I am only using the converter to check my answers. Our professor will not credit work without solution.

    From what I understand,

    If decimal is positive:
    STEP 1: Convert magnitude to binary.
    STEP 2: Pad 0's to desired bitsize.

    If decimal is negative:
    STEP 1: Convert magnitude to binary.
    STEP 2: Pad 0's to desired bitsize.
    STEP 3: Invert bits to achieve 1's-complement.
    STEP 4: Add 1 to achieve 2's-complement.

    Since 16 is positive, I simply converted it to binary by repeated division yielding (10000) base 2.
    I no longer padded 0's since it is already 5-bits (containing 5 digits).

    Can somebody explain to me why the online converter is returning an error (which also I strongly believe is the correct answer)?

    I would love to understand this concept. Thank you.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,918
    Rep Power
    1045
    Hi,

    in the two's complement representation, non-negative numbers are always displayed with a leading "0". Otherwise, you couldn't tell the numbers apart. Your "10000" could be either 16 (if interpreted as a simple binary representation) or -16 (if interpreted as a two's complement). So that obviously doesn't work.

    +16 simply exceeds the range of possible numbers. 5 digit let us display 2^5 = 32 numbers. This space is evenly divided into 16 non-negative numbers (0 to 15) and 16 negative numbers (-1 to -16). So the range is from -16 to +15. The representations of the non-negative numbers start with a "0". And those of the negative numbers with a "1".

    Comments on this post

    • kathy025 agrees : Well explained.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    30
    Rep Power
    1
    Thank you. This is very helpful:
    Originally Posted by Jacques1
    in the two's complement representation, non-negative numbers are always displayed with a leading "0". Otherwise, you couldn't tell the numbers apart. Your "10000" could be either 16 (if interpreted as a simple binary representation) or -16 (if interpreted as a two's complement). So that obviously doesn't work.
    We are required to explain why the conversion is not possible, this is what I wrote (before I read above explanation):
    2^5 = 32
    The signed range in decimal is -16 to +15, which is 32 digits including 0.
    Therefore, +16 is out of range.
    ...which appears to be in the second paragraph. I wasn't sure before, so thanks a lot for confirming.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,918
    Rep Power
    1045
    The wording isn't correct, though. You're talking about "32 digits". But what you mean is 32 numbers.

    A digit is a single symbol of a number representation. For example, the binary representation "10111" (which represents the number 23) consists of 5 digits.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    30
    Rep Power
    1
    Good catch. Would it be proper if I called it "32 integers" as well?

IMN logo majestic logo threadwatch logo seochat tools logo