How do I convert signed decimal to binary

  • Thread starter Thread starter XodoX
  • Start date Start date
  • Tags Tags
    Binary Convert
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 8K views
XodoX
Messages
195
Reaction score
0
1. Convert the following numbers to 32-bit two's complement binary numbers.

-22 , -100





Can someone explain this to me? I thought I just convert the +22, which is 10110 and add it to all the 1's. Like,

111111...10110 and positive would be 00000000...10110. But that's not how it works.
 
Physics news on Phys.org
XodoX said:
1. Convert the following numbers to 32-bit two's complement binary numbers.

-22 , -100





Can someone explain this to me? I thought I just convert the +22, which is 10110 and add it to all the 1's. Like,

111111...10110 and positive would be 00000000...10110. But that's not how it works.

You need to count backwards from 0000000 to 1111111 to 1111110, etc. Count with 0's for the negative numbers, not with 1's:

http://en.wikipedia.org/wiki/2s_complement

.
 
I don't knwo what you mean. I don't even shave the right numbers it seem.s
 
XodoX said:
I don't knwo what you mean. I don't even shave the right numbers it seem.s

Did you read the wiki link?
 
Yeah, I don't get it. I read it before I posted this thread.
 
Right, here's how it's done.

1) You were right to convert the +ve value to binary to give 000...010110
2) Flip the bits (every 1 becomes a 0)
3) add 1 to the answer.

The bit that intrigued me was the adding 1 part but if we take 0 as an example and use 8 bits then:

0(dec) = 00000000(bin)
flip to give 11111111(dec)
If you don't add 1 then +0 and -0 are different so we add 1 and discard the carry to give 00000000.

-1 would then be 11111111
-2 becomes 11111110 and so on.

To show the usefulness of this let's have a look at 5 - 2:

We treat it as 5 + (-2) as you can in decimal:

00000101
11111110 +
-----------
00000011 (overfow set but ignored here)

So there you go, a whistlestop tour of 2's complement!
 
Thank you. So,

0110 1100 0001 0000 would be

0110 1100 0001 0001

I just haver seen an example where they did it totally differently.
 
Not quite, to save any issues on here (and make sure I'm not doing any homework) I'll use an example I'm making up:

0011 0110 1001 0011
1100 1001 0110 1100 - Flipped
1100 1001 0110 1101 - With the 1 added, and that's the -ve value of 0011 0110 1001 0011 using 2's complement.
 
Last edited: