10 bit 2's complement of positive numbers

11.9k Views Asked by At

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

1

There are 1 best solutions below

0
On BEST ANSWER

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.