2's complement of binary numbers and registers

In summary: 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.
  • #1
vysero
134
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
  • #2
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.
 
  • #3
Well I was a bit confused because it is a negative number. So I was not sure if it should be 00000111 or 10000111.
 
  • #4
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?"
 
  • #5
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.
 
  • #6
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.
 

FAQ: 2's complement of binary numbers and registers

What is the purpose of using 2's complement in binary numbers?

The 2's complement is used in binary numbers to represent negative numbers. It is a way to perform subtraction using only addition, making it more efficient for computers to handle.

How is the 2's complement of a binary number calculated?

To calculate the 2's complement of a binary number, you first find the 1's complement by flipping all the bits (0s become 1s and vice versa). Then, you add 1 to the 1's complement. The resulting number is the 2's complement.

Can the 2's complement be used for both positive and negative numbers?

Yes, the 2's complement can represent both positive and negative numbers. The first bit (left-most) of a binary number is used as the sign bit - 0 for positive and 1 for negative. This allows for a wider range of numbers to be represented compared to other methods.

How is the 2's complement used in computer registers?

The 2's complement is used in computer registers to store and manipulate signed numbers efficiently. The register will have a fixed number of bits, and the 2's complement representation allows for easy conversion between positive and negative numbers.

Are there any limitations or drawbacks to using 2's complement in binary numbers?

One limitation of using 2's complement is that there is no way to represent the number 0 with a sign. This means that one extra bit is required to represent all numbers in a given range. Additionally, there is a possibility for overflow when performing calculations with large numbers.

Similar threads

Replies
1
Views
848
Replies
2
Views
2K
Replies
2
Views
1K
Replies
8
Views
2K
Replies
2
Views
2K
Replies
24
Views
6K
Back
Top