2's complement of binary numbers and registers

  • Thread starter Thread starter vysero
  • Start date Start date
  • Tags Tags
    Binary Numbers
Click For Summary
To insert a 2's complement number into a larger register while maintaining its value, padding with leading zeros is appropriate for positive numbers, but for negative numbers, the most significant bit should be extended. For instance, the 3-bit representation of -1 (111) becomes 11111111 in an 8-bit register. When converting other negative numbers, such as -6, the same principle applies, extending the sign bit to fill the register. The discussion emphasizes that the additional bits beyond the original size do not affect the value in the context of the original bit-width. Understanding how to represent negative numbers correctly in various bit-widths is crucial for accurate binary arithmetic.
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
vysero said:
Lets say I want to put that number: 111, into an 8 bit register. Can I simple pad the number with leading zeros: 00000111?

Yes. I don't know what else you could do with those extra 5 bits other than set them to 0.
 
Well I was a bit confused because it is a negative number. So I was not sure if it should be 00000111 or 10000111.
 
Ah, I see. -1 in an eight bit register is 11111111. You should have asked simply, "how do I represent -1 in an 8 binary bit register?"
 
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 24 ·
Replies
24
Views
7K