How is this sum equal in unsigned binary and twos complement binary? - binary

I'm reading through a computer architecture book and came across the following:
Assume you have a 8-bit cell. So there are 256 possible integer values. Non-negatives run from 0 to 127. Assuming two's complement binary representation, what is the sum of 97 + 45?
Unsigned is clearly 142, you can do it as 97 + 45 in decimal, or:
0110 0001
0010 1101 ADD
--------------
1000 1110
But when you perform two's complement, you take that result (1000 1110) and determine that it is negative since the sign bit is 1. Then take the one's complement of it:
NOT 1000 1110 = 0111 0001
Then determine its two's complement:
0111 0001
0000 0001 ADD
--------------
0111 0010
This number is 114 but because our original number had a 1 in the sign bit, it's -114.
Question(s):
Why go through all the trouble of having the two's complement to find -114? Since 97 and 45, why not just find the sum of the two positive integers as an unsigned value which fits within the range of an 8-bit cell (1111 1111 being 255). Is it just because the question asks for the two's complement?
Is -114 equivalent to 142? I believe so if you take the two's complement number line, yo get 142-256 which is -114. From this, I dont understand why you would want to even use two's complement if you are summing two positive values!

A 1's complement just means flip all the bits, a 2's complement means negate the value. So 1's complement for 8-bit results effectively in 255 - x and 2's in 256 - x. You achieve the result of a 2's complement by doing a 1's complement and adding 1.
The 142 in 8-bit is equal to -114. Don't get too confused about it.

Related

What is a 32-bit two's complement?

I'm really confused about the term "32-bit twos complement"
If I have the number 9, what is the 32-bit twos complement?
9 = 1001
Two's complement = 0111
32-bit 9 = 0000 0000 0000 0000 0000 0000 0000 1001
Two's complement = 1111 1111 1111 1111 1111 1111 1111 0111
That result is so ridiculous! It's way too large! Is that really how you're supposed to do it?
The most common format used to represent signed integers in modern computers is two's complement. Two's complement representation allows the use of binary arithmetic operations on signed integers.
Positive 2's complement numbers are represented as the simple binary.
Negative 2's complement numbers are represented as the binary number that when added to a positive number of the same magnitude equals zero.
Your 2's complemented output is equivalent to -9 (negative 9).
Edited:
You are asked to perform 32-bit operation hence it should be 1111 1111 1111 1111 1111 1111 1111 0111
For signed number, leftmost bit represents the sign of the number. If leftmost bit (LSB) is 1 then the number is negative otherwise it's positive. So, your 32-bit 2's complemented number is negative and it's -9. Simply, performing 2's complement on a number is equivalent to negating it
i.e. it makes a positive number into negative and vice versa.
For more browse the link:
http://www.tfinley.net/notes/cps104/twoscomp.html

Binary into 2's compliment

if I convert the binary number 000000 into 2's compliment I will get
1's compliment (invert) = 111111
2's compliment (add +1) = here I run into a problem, does this return 000000 and the 1 gets discarded or does this return 1000000?
Thanks in advance!
It discards the 1. The condition is similar to the Arithmetic Overflow.
Strength of two's complement is that it helps us retain a binary representation when signed numbers are to be considered, because in mathematics, the value of 0 is same as that of -0. If we have to give up one entire bit just for sign, then, in a 4-bit word, 0000 would denote 0 and 1000 would denote -0, wasting one representation. The two's complement helps get rid of this. If we assume 4-bit words:
val -val bits of val two's complement bits of -val (1's complement + 1)
0 0 0000 0000 (1111+0001)
1 -1 0001 1111 (1110+0001)
2 -2 0010 1110 (1101+0001)
3 -3 0011 1101 (1100+0001)
...
7 -7 0111 1001 (1000+0001)
8 -8 (no rep) 1000 (0111+0001)
(note that for -8 you have one's complement of 8 in an unsigned manner, i.e.8 = 1000 and hence its one's complement is 0111).
Thus you gain a representation for -8 by making 0 and -0 have the same bit pattern, i.e. 0000. Using this, for n bits, we can represent all integer values between -2^(n-1) to 2^(n-1)-1.

How can I check a negative solution of a twos complement subtraction equation?

Hey guys I'm still trying to get the hang of twos complement arithmetic and I can get the correct answer, since I'm working on practice problems with solutions.
When I take the answer that's in binary, I can't seem to equate it out to the decimal answer before applying twos complement and adding the numbers.
000100-111001 In decimal it's 4 - 57= -53
Becomes
0001000+000111 which would be 4 + (-57)?
Giving a solution of 001011
How can 001011 be proven as equaling -53?
Thanks!
To answer your question, first 001011 is not equal to -53. This is the wrong answer. We know it must be positive since the highest order bit is a 0, not a 1. 001011 is actually equal to 11 (in base ten).
Let's do 4 - 57 now as an example. This is the same as 4 + (-57). Converting to binary (I will use just a byte for this example) we get: 4 is 0000 0100, 57 is 0011 1001. To convert 57 to negative 57 use two's complement:
1. Negate it: 1100 0110
2. Add one: 1100 0111So now we get the following equation:
0000 0100
+ 1100 0111
------------
1100 1011
We achieve the answer by simply adding down the rows. The answer we have gotten is 1100 1011. We know it is negative because the highest order bit (which here is leftmost) is a 1. To find its magnitude, we apply two's complement:
1. Negate: 0011 0100
2. Add one: 0011 0101
And this is equal to 53 in base ten.
Another way to see if it is correct is to add it with the positive version of the number.
1100 1011
+ 0011 0101
------------
10000 0000
Since two's complement is defined as the number subtracted from 2^n where n is the number of bits, you will always get this result for the sum. Knocking off the 1s digit it's interesting how what remains is just 0 - and any number plus its negative is zero.

converting decimal to signed binary

Let's say I want to convert "-128" into binary.
From what I understand, I get the binary representation of "128", invert the bits and then add 1.
So 128 = 10000000
So the "inverse" is 01111111
So and "01111111" + "1" = "10000000" which is "-0" isn't it?
My textbook makes this seem so easy but I can't figure out what I'm doing wrong. Thanks for the help.
No, that's definitely -128 (in two's complement anyway, which is what you're talking about given your description of negating numbers). It's only -0 for the sign/magnitude representation of negative numbers.
See this answer for details on the two representations plus the third one that C allows, one's complement, but I'll copy a snippet from there to keep this answer as self-contained as possible.
To get the negative representation for a positive number, you:
invert all bits then add one for two's complement.
invert all bits for one's complement.
invert just the sign bit for sign/magnitude.
You can see this in the table below:
number | twos complement | ones complement | sign/magnitude
=======|=====================|=====================|====================
5 | 0000 0000 0000 0101 | 0000 0000 0000 0101 | 0000 0000 0000 0101
-5 | 1111 1111 1111 1011 | 1111 1111 1111 1010 | 1000 0000 0000 0101
You should be aware that there is no 128 in 8-bit two's complement numbers, the largest value is 127.
Where the numbers pass the midpoint is where the "clever" stuff happens:
00000000 -> 0
00000001 -> 1
: :
01111110 -> 126
01111111 -> 127
10000000 -> -128
10000001 -> -127
: :
11111110 -> -2
11111111 -> -1
because adding the bit pattern of (for example) 100 and -1 with an 8-bit wrap-around will auto-magically give you 99:
100+ 0 0110 0100
1- 0 1111 1111
===========
1 0110 0011 99+ (without that leading 1)
It depends on what your binary representation is -- ones complement, twos complement, sign-magnitude, or something else. The "invert bits and add 1" is correct for twos complement, which is what most computers these days use internally for signed numbers. In your example, "10000000" is the 8-bit twos-complement representation of -128, which is what you want. There is no such thing as -0 in twos complement.
For sign-magnitude, you negate by flipping the sign bit. For ones complement, you negate by inverting all bits.

Decimal to binary number conversion

What is the binary form of -10? How it is calculated?
To convert -10 (decimal) to binary:
Repeatedly divide the absolute value (|-10| = 10) of the number by 2 until you get 0 in the quotient:
(10 / 2 = 5 R 0)
(5 / 2 = 2 R 1)
(2 / 2 = 1 R 0)
(1 / 2 = 0 R 1) // zero is the value in the quotient so we stop dividing
Place the remainders in order to obtain the binary equivalent:
1010
For an 8-bit cell the answer is 0000 1010, 16-bit cell 0000 0000 0000 1010, and so on.
Take the one's complement by inverting the bits (we will assume an 8-bit cell holds the final value):
0000 1010
1111 0101 // bits are inverted
Now take the 2's complement by adding 1:
1111 0101
+ 1
----------
1111 0110 // final answer
What happens with a 4-bit cell?
The one's complement would be:
1010
0101 // inverted bits
Taking the 2's complement produces:
0101
+ 1
----
0110 // final answer for a 4-bit cell
Since the number should be negative and the result does not indicate that (the number begins with 0 when it should begin with a 1) an overflow condition would occur.
Take the binary form of 10, invert all the bits, and add one.
10 0000 1010
invert 1111 0101
add 1 1111 0110
Follow this link. You can find the binary form of a negative number say -n by finding out the two's complement of n.
Your question has no "right" answer. First, you have to define the representation you want to use. Do you want to use two's complement, ones' complement, sign-magnitude, or something else? Then you have to define how many bits to use.
Let's say we are working with 5-bits wide representations.
+10: 0 1 0 1 0
Let's look at sign-magnitude system. In this system, the most significant bit (bit 5) is 1 if a number is negative, and 0 otherwise. The rest of the bits represent the magnitude (absolute value) of the number.
So we get:
-10: 1 1 0 1 0 (sign-magnitude, 5 bits)
Let's look at ones' complement now. Here, a negative number is represented by just changing 1s to 0s and vice-versa (hence the name ones' complement—you complement a number with respect to a long sequence of 1s).
So we get:
-10: 1 0 1 0 1 (ones' complement, 5 bits)
Finally, let's look at two's complement system. In this, we take a number in its ones' complement system, and then add 1 to it.
So we get:
-10: 1 0 1 0 1
1
---------
1 0 1 1 0 (two's complement, 5 bits)
---------
Thus, the binary representation of a negative number depends upon the system we use, and the number of bits available to us.
Also, you might have noticed the position of the apostrophe in ones' complement and two's complement. Why is is not one's complement or twos' complement? Then answer, from Knuth:
A two's complement number is complemented with respect to a single power of 2, while a ones' complement number is complemented with respect to a long sequence of 1s. Indeed, there is also a "twos' complement notation," which has radix 3 and complementation with respect to (2...22)3