2
$\begingroup$

When doing this for a negative number, like -213 for example, I successfully found the answer by doing $(2^9 + -327)$ => $(2^9 - 213)$ taking the result of that, and using the division by 2 method, adding on leading zeros to make it ten bits, the final left most bit being a 1 since it's negative. When I tried to do something similar with a positive number trying ($2^9 + \text{posnumber}$) and then continuing on the same as before, I wasn't able to get the correct answer. I thought maybe I had to subtract in both cases and just make the leading digit 0 for it being positive but that wasn't giving me the right answer either.

So can someone tell me how I'm supposed to handle these and explain this better?

(I know you can do something with flipping bits and adding one but am not supposed to do that here.)

$\endgroup$
  • $\begingroup$ Because I did the negative ones fine using one method and was successful, then tried to adapt and use it for positive numbers but I can't figure out how to do the positive ones. $\endgroup$ – windy401 Sep 18 '16 at 22:43
  • $\begingroup$ That and just positive numbers in general. I figured out how to do the negatives, but I'm having trouble with the positive numbers. $\endgroup$ – windy401 Sep 18 '16 at 22:43
2
$\begingroup$

To convert a positive decimal number to $10$-bit two's-complement form, we:

  • $(1.)$ Convert decimal to binary (recall that there is a range we can support with $n$-bits)
  • $(2.)$ Pad to size of number of bits desired

Example 1: Convert $213$ to $10$-bit $2's$-complement

  • $(1.)$ $213_{10} = 1101~0101_2$
  • $(2.)$ Pad to size $10$-bits $213_{10} = \bbox[5px,border:2px solid red]{00~1101~0101_2}$

To convert a negative decimal number to $10$-bit two's-complement form, we:

  • $(1.)$ Convert decimal to binary (recall that there is a range we can support with $n$-bits)
  • $(2.)$ Pad to size of number of bits desired
  • $(3.)$ Invert all the bits
  • $(4.)$ Add $1$

Example 2: Convert $-213$ to $10$-bit $2's$-complement

  • $(1.)$ $213_{10} = 1101~0101_2$
  • $(2.)$ Pad to size $10$-bits: $213_{10} = 00~1101~0101_2$
  • $(3.)$ Invert all the bits: $11~0010~1010_2$
  • $(4.)$ Add $1$: $11~0010~1010_2 + 1_2 = \bbox[5px,border:2px solid red]{11~0010~1011_2}$

Of course, this process works with any size, just always be sure to use the correct number of bits and that those provide enough range for the numbers in question, for example, for $8$-bit binary, we can represent the range of signed numbers $2^{n-1}$, that is $-128 ~ \mbox{to}~ 127$.

The process to go backwards to convert a negative two's complement number back to decimal is:

  • Flip all the bits
  • Add $1$, and
  • Interpret the result as a binary representation of the magnitude and add a negative sign

Try this on the above result and make sure you understand how to go both ways.

Lastly, try using hex and seeing if you can do it that way, it is typically easier, but you must always be careful.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.