Binary Subtraction with 2's Complement - binary

I need help subtracting with binary using 2's representation and using 5 bits for each number:
1) -9 -7 = ? Is there overflow?
-9 = 01001 (2's complement = 10111) and -7 = 00111 (2's complement = 11001)
Now we need to add because we're using 2's complement
10111
+11001
= 100000 But this answer doesn't make sense. Also, I'm assuming there's overflow because there are more than 5 bits in the answer.
2) 6 - 10, same process as before. Negative binary numbers don't make sense to me

1) -9 - 7
-9 - 7 = -9 + -7
9 (binary) = 01001
-9 (2's complement) = 10111
7 (binary) = 00111
-7 (2's complement) = 11001
10111 +
11001 =
110000
This doesn't fit into 5 bits. Removing the overflow we get 10000, which is -16 (binary).
2) 6 - 10
6 - 10 = 6 + -10
6 (binary) = 00110
10 (binary) = 01010
-10 (2's complement) = 10110
00110 +
10110 =
11100
This fits into 5 bits and is -4 (binary).

10111 + 11001 is not 100000 but 110000.
1111
10111
+ 11001
-----
110000

Answer to the 1st question is wrong. To find -9-7 using two's complement, we need follow these steps:
STEP:1 Convertion of first number
1st the binary conversion of 9: 01001
2nd find the complement of binary: 10110
Add 1 to the binary complement: 10110
+1
-----
10111
STEP 2: Convertion of second number
1st the binary conversion of 7: 00111
2nd find the complement of binary: 11000
Add 1 to the binary complement: 11000
+1
-------
11001
STEP 3: Adding
Now add the two outputs -9 + (-7): 10111
+11001
--------
110000
The most important thing is checking the answer whether it is correct or not.
you may use index for the binary digits: 7 6 5 4 3 2 1 0
1 1 0 0 0 0
find the 2 raised to the power of each index having 1 digit.
(-)2^5 + 2^4
*Note (-) is used because in two's complement, the most significant bit (the bit with the highest index) is a sign bit -2^5 + 2^4 = -32 + 16 = -16
which is the correct answer for -9-7=-16. For this reason 2's complement become a popular way of representing negative number. For sign magnitude we need to assume a sign bit, which is hard to implement in a computer, and for 1's complement we need to add 1 to find the correct answer.

Related

How to add 1 for two's complement when you have a 1 in the first bit? [duplicate]

Using six-bit one's and two's complement representation I am trying to solve the following problem:
12 - 7
Now, i take 12 in binary and 7 in binary first.
12 = 001100 - 6 bit
7 = 000111 - 6 bit
Then, would I then flip the bit for two's complement and add one?
12 = 110011 ones complement
+ 1
-------
001101
7 = 111000 ones complement
+ 1
---------
111001
then, add those two complement together
001101
+111001
-------
1000110 = overflow? discard the last digit? If so I get 5
Now, if I have a number like
-15 + 2
I would then add a sign magnitude on the MSB if it's a zero?
like:
-15 = 001111 6 bit
Would I add a 1 at the end here before I flip the bits?
= 101111
Using two's complement to represent negative values has the benefit that subtraction and addition are the same. In your case, you can think of 12 - 7 as 12 + (-7). Hence you only need to find the two's complement representation of -7 and add it to +12:
12 001100
-7 111001 -- to get this, invert all bits of 7 (000111) and add 1
----------
5 1000101
Then discard the carry (indicates overflow), and you have your result: 000101 which equals to 5 as expected.
For your example of -15 + 2, simply follow the same procedure to get the two's complement representation of -15:
15 001111
110000 -- inverted bits
110001 -- add 1
Now do the addition as usual:
-15 110001
2 000010
-----------
res 110011
To see that res indeed equals -13, you can see that it is negative (MSB set). For the magnitude, convert to positive (invert bits, add 1):
res 110011
001100 -- inverted bits
001101 -- add 1
Hence the magnitude is 13 as expected.
No. The algorithm for two's complement doesn't change based on where the negative value is.

Need help adding with Twos Complement notation

I'm having slight confusion over two's complement. I have reviewed What is “2's Complement”?.
I'm trying to add -2 + -3 = -5. Here's my thought process:
+2 = 0010
-2 = 1110 # in twos complement
+3 = 0011
-3 = 1101 # in twos complement
1101
+1101
-----
10010 # What is this?
I know -5 is 1011 in Two's Complement. But I'm not sure what I did when I added -2 + -3 in the example above.
An explanation of the process to add -2 + -3 would be appreciated. Thank you for your assistance!
In 4 bit 2'scomplement -2 is 1110 and +3 is 0011 so
11110 carry
1110 -2
+0011 +3
----
10001 which is 0001 or simply 1 ignoring the carry in bit 5
Stepping through the process from the right to the left:
1 + 0 results in 1 with no carry
1 + 1 results in 0 with a carry of 1
1 + 0 + the carry of 1 results 0 with a carry of 1
1 + 0 + the carry of 1 results in 0 with a carry of 1
just the carry of 1 results in 1 with nothing more to carry
For reference see the Wikipedia article on 2's complement particularly the section on addition at https://en.wikipedia.org/wiki/Two%27s_complement#Addition. There are a number of online 2's complement calculators to help with conversions and one of them is at http://www.exploringbinary.com/twos-complement-converter/
Let me know if you want to see how -3 + -3 is done, since that is what you attempted. It is a similar process, but be sure start with bit length large enough to avoid overflow as determined when the leftmost two bits in the carry row have different values.

subtracting two's complement for beginner

Guys need some help to solve a question. I am trying to learning Complement notation which is in my bca course. Recently i finished the 2's complement notation chapter, ans stuck at question.
Can some one describe me that how to subtract
a)10 from 14
b)14 from 10
using 2's complement notation.
What i have done is
Above numbers in binary.
10=1010
14=1110
two's complement of the two numbers.
1010=>0101+1=0110
1110=>1110+1=1111
Now i am stuck how will i solve the
a)Subtraction of 10 from 14
b)Subtraction of 14 from 10
Please use descriptive way so i can understand each step for both subtraction. Thanks in advance.
At last, we have found the solution
10 in binary 00001010
2's complement of 10 is 0110
14 in binary 00001110
2's complement of 14 is 0010
4 in binary 0000 0100
2's complement of 4 is 1111 1100
2's Complement Subtraction=>
10 - 14 = (-4) 0000 1010 = +10
+ 1111 0010 = -14
1111 1100 = -4
2's Complement Addition=>
14 - 10 = (+4) 0000 1110 = +14
+ 1111 0110 = -10
10000 0100 = +4
Once you are done with 2' complement,
You just need to add it.
Subtraction of 14 from 10
1010=>0101+1=0110 [This means -10]
14 + (-10)
1110
+
0110
0100 [Do not worry about the carry]
Subtraction of 10 from 14
10=1010 14=1110
2's complement of 1110 is 0010
1 0 1 0 + 0 0 1 0 = 1 1 0 0
2's complement of 1100 is 0100
10 - 14 = -4
1010 - 1110 =0100

The binary equivalent of the decimal number 104

Ok,so I know that the binary equivalent of 104 is 1101000.
10=1010
4=0100
so , 104=1101000 (how to get this??how these both mix together and get this binary?)
And from the example here...
the octets from "hellohello" are E8 32 9B FD 46 97 D9 EC 37.
This bit is inserted to the left which yields 1 + 1101000 = 11101000 ("E8").
I still understand this part , but how to convert 11101000 to E8?
I'm so sorry for all these noob questions , I just learn it yesterday , I googled and search for a whole day but still not really understand the concept...
Thank you.
Ok,so I know that the binary equivalent of 104 is 1101000.
10=1010
4=0100
You can't break apart a number like 104 into 10 and 4 when changing bases. You need to look at the number 104 in its entirety. Start with a table of bit positions and their decimal equivalents:
1 1
2 10
4 100
8 1000
16 10000
32 100000
64 1000000
128 10000000
Look up the largest decimal number that is still smaller than your input number: 104 -- it is 64. Write that down:
1000000
Subtract 64 from 104: 104-64=40. Repeat the table lookup with 40 (32 in this case), and write down the corresponding bit pattern below the first one -- aligning the lowest-bit on the furthest right:
1000000
100000
Repeat with 40-32=8:
1000000
100000
1000
Since there's nothing left over after the 8, you're finished here. Sum those three numbers:
1101000
That's the binary representation of 104.
To convert 1101000 into hexadecimal we can use a little trick, very similar to your attempt to use 10 and 4, to build the hex version from the binary version without much work -- look at groups of four bits at a time. This trick works because four bits of base 2 representation completely represent the range of options of base 16 representations:
Bin Dec Hex
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 F
The first group of four bits, (insert enough leading 0 to pad it to four
bits) 0110 is 6 decimal, 6 hex; the second group of four bits, 1000 is
8 decimal, 8 hexadecimal, so 0x68 is the hex representation of 104.
I think you are making some confusions:
104 decimal is 1101000 which is not formed by two groups splitting 104 into 10 and 4.
The exception is for hex numbers that can be formed by two groups 4 binary numbers (2^4 = 16).
So 111010000 = E8 translates into 1110 = E and 8 = 10000. 1110 (binary) would be 14 (decimal) and equivalent of E (hex).
Hex numbers go from 0 to 15 (decimal) where:
10 (decimal) = A (hex)
11(decimal) = B(hex)
...
15(decimal) = F(hex)
What you're missing here is the general formula for digital numbers.
104 = 1*10^2 + 0*10^1 + 4*10^0
Similarly,
0100b = 0*2^3 + 1*2^2 + 0*2^1 + 0*0^0
And for a hexidecimal number, the letters A-F stand for the numbers 10-15. So,
E8 = 14*16^1 + 8*16^0
As you go from right to left, each digit represents the coefficient of the next higher power of the base (also called the radix).
In programming, if you have an integer value (in the internal format of the computer, probably binary, but it isn't relevant), you can extract the right most digit with the modulus operation.
x = 104
x % 10 #yields 4, the "ones" place
And then you can get "all but" the rightmost digit with integer division (integer division discards the remainder which we no longer need).
x = x / 10 #yields 10
x % 10 #now yields 0, the "tens" place
x = x / 10 #yields 1
x % 10 #now yields 1, the "hundreds" place
So if you do modulus and integer division in a loop (stopping when x == 0), you can output a number in any base.
This is basic arithmetic. See binary numeral system & radix wikipedia entries.

Adding and subtracting two's complement

Using six-bit one's and two's complement representation I am trying to solve the following problem:
12 - 7
Now, i take 12 in binary and 7 in binary first.
12 = 001100 - 6 bit
7 = 000111 - 6 bit
Then, would I then flip the bit for two's complement and add one?
12 = 110011 ones complement
+ 1
-------
001101
7 = 111000 ones complement
+ 1
---------
111001
then, add those two complement together
001101
+111001
-------
1000110 = overflow? discard the last digit? If so I get 5
Now, if I have a number like
-15 + 2
I would then add a sign magnitude on the MSB if it's a zero?
like:
-15 = 001111 6 bit
Would I add a 1 at the end here before I flip the bits?
= 101111
Using two's complement to represent negative values has the benefit that subtraction and addition are the same. In your case, you can think of 12 - 7 as 12 + (-7). Hence you only need to find the two's complement representation of -7 and add it to +12:
12 001100
-7 111001 -- to get this, invert all bits of 7 (000111) and add 1
----------
5 1000101
Then discard the carry (indicates overflow), and you have your result: 000101 which equals to 5 as expected.
For your example of -15 + 2, simply follow the same procedure to get the two's complement representation of -15:
15 001111
110000 -- inverted bits
110001 -- add 1
Now do the addition as usual:
-15 110001
2 000010
-----------
res 110011
To see that res indeed equals -13, you can see that it is negative (MSB set). For the magnitude, convert to positive (invert bits, add 1):
res 110011
001100 -- inverted bits
001101 -- add 1
Hence the magnitude is 13 as expected.
No. The algorithm for two's complement doesn't change based on where the negative value is.