2's complement of binary numbers and registers

  • Thread starter Thread starter vysero
  • Start date Start date
  • Tags Tags
    Binary Numbers
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
5 replies · 2K views
vysero
Messages
134
Reaction score
0

Homework Statement



I want to know how to insert a 2's complement number into a larger register and keep the same value. I would like to know of some pitfalls that I might run into using a generalized method, if any.

Homework Equations

The Attempt at a Solution



If I have say 1 in a 3 bit binary system, 001 and I take the 2's complement of the number I will get 111 or -1 in a 3 bit binary system. Let's say I want to put that number: 111, into an 8 bit register. Can I simple pad the number with leading zeros: 00000111?
 
Physics news on Phys.org
Well I was a bit confused because it is a negative number. So I was not sure if it should be 00000111 or 10000111.
 
Well I would like how to know how to represent any number in such a way. Say my number is 010. If I take the two's complement of the number then I get 110 which is -6. How can I represent 110 in an eight bit register or a 16 bit register and so on.
 
vysero said:
How can I represent 110 in an eight bit register or a 16 bit register and so on.
Just extend the most significant bit.
As 8 bits: 1111 1110
As 16 bits: 1111 1111 1111 1110
As 32 bits: 1111 1111 1111 1111 1111 1111 1111 1110
And so on.
If you add 2 (or 102) to of these numbers you'll get 0 in bits 0 through 7 for the first number, 0 in bits 0 through 15 for the second number, and 0 in bits 0 through 31 for the third number. Bit 8 will be 1 in the first number, bit 16 will be 1 in the second number, and bit 32 will be 1 in the third number, but these bits aren't in an 8-bit number, a 16-bit number, or a 32-bit number, respectively.

These additions show that -2 + 2 = 0, at least as far as 8-, 16-, or 32-bit addition are concerned.