• Happy 20 YEARS to us!

Binary Arithmetic (1 Viewer)

Pastafarian

New Member
Joined
Jul 2, 2007
Messages
22
Gender
Undisclosed
HSC
2007
Taken from Heinemann HSC
10.3
4) Perform these subtractions using the twos complement method. Show all working. Write answers in twos complement and sign plus modulus form.

d.) 0000 0110 - 1000 1110

my answer: ones complement 0111 0001
twos complement 0111 0010
difference 0111 1000
This cannot be right as the expected answer should be negative so i took the ones and twos complement of the difference:

ones : 1000 0111
twos : 1000 1000
modulus = 1000 1000, which is -8..........answer asking for 6-142 = -136

The solutions found here: http://www.hi.com.au/softwaredesign/ state

The second number 1000 1110
One’s complement 0111 0001
Add one 0000 0001
Two’s complement 0111 0010
Add first number 0000 0110
Difference 0111 1000

Answer is positive (as expected) so 0111 1000 is also sign plus modulus form.


Help please.
Thanks.


EDIT: gah, 16 views but no-one has come across this problem before??!!
 
Last edited:

meowz0r

New Member
Joined
Sep 10, 2006
Messages
21
Location
Sydney
Gender
Male
HSC
2007
sorry...being kicked outta the library...will respond in 30 minutes! haha XD
 

meowz0r

New Member
Joined
Sep 10, 2006
Messages
21
Location
Sydney
Gender
Male
HSC
2007
EDIT: for clarity, THIS IS ALL WRONG, DO NOT READ IT xd. Two's complement can only represent 127 to -128, so -142 is outside the representable bounds.

ok,

The idea of taking the two's complement of a subtraction is to turn it from a subtraction into the addition of two numbers, which is easier for the computer to understand.

So, we're turning the subtraction into a subtraction:
0000 0110 -
1000 1110

or in decimal 6 - 142

taking the two's complement of the first term by saving the first 1 and swapping all other 0s with 1s and 1s with 0s,

1000 1110 becomes 0111 0010

Thus, the equation becomes

0000 0110 +
0111 0010

or decimal, 6 + 114

Using the results 0 + 0 = 0, 1 + 0 = 1, 1 + 1 = 0 (but carry the one), 1 + 1 + 1 = 1 (carry the one), the equation gives us

0000 0110 +
0111 0010
--------------
0111 1000

decimal:
= 114 + 6
= 120

Now, the two's complement of 120 is -136, so simply convert it to binary

0111 1000 --> 1000 1000


not sure that answered your question...perhaps you forgot to subtract 128 when you got -8?
 
Last edited:

Pastafarian

New Member
Joined
Jul 2, 2007
Messages
22
Gender
Undisclosed
HSC
2007
meowz0r said:
ok,
Now, the two's complement of 120 is -136, so simply convert it to binary

0111 1000 --> 1000 1000


not sure that answered your question...perhaps you forgot to subtract 128 when you got -8?
Why isn't the twos complement of -136, 120? shouldn't it be the other way around?

meowz0r said:
0111 1000 --> 1000 1000


not sure that answered your question...perhaps you forgot to subtract 128 when you got -8?
What have you done here?
 

meowz0r

New Member
Joined
Sep 10, 2006
Messages
21
Location
Sydney
Gender
Male
HSC
2007
Pastafarian said:
Why isn't the twos complement of -136, 120? shouldn't it be the other way around?

What have you done here?
Omg, i'm mortified! My own ignorance on display :S. Reading up on the two's complement system, it seems the first bit is known as the "highest order bit", giving it the negative value. For 8 bits, this means the 8th bit represents -128. It seems that 8 bits can infact only represent values from 127 to -128, oh how wrong i was!

Ok, try #2.

Firstly, in decimal:

The original equation: 6 - ( -114) which is converted to 6 + 114 = 120

In binary:

0000 0110 - 1000 1110 --> 0000 0110 + 0111 0010 (using two's complement)

0000 0110 +
0111 0010
0111 1000

So the answer is 0111 1000.

The final conversion of my previous post isn't necessary - a negative subtracted from a positive when both are positive gives a positive result.

I'm so sorry! Least i know now..better then realising in the exam huh...>< thanks Pastafarian, i owe you one XD Hope that makes more sense...
 

Pastafarian

New Member
Joined
Jul 2, 2007
Messages
22
Gender
Undisclosed
HSC
2007
This is really giving me the shits, i had the same problem, we were taught that the 8th bit is used to represent negative or positive, made me confused when the teacher isn't teaching it that way. Saying it will overflow etc.

Oh also can you explain to me why 1000 0000 is -128?
 

meowz0r

New Member
Joined
Sep 10, 2006
Messages
21
Location
Sydney
Gender
Male
HSC
2007
Pastafarian said:
This is really giving me the shits, i had the same problem, we were taught that the 8th bit is used to represent negative or positive, made me confused when the teacher isn't teaching it that way. Saying it will overflow etc.

Oh also can you explain to me why 1000 0000 is -128?
The way the samuel davis book explains it is that integers can be represented one of two ways: using sign and modulus OR two's (and one's) complement.

Using the two's complement method, the first digit is ALWAYS negative, ie if the integer was 9 bits, then the highest order bit would be -256, and you would be able to represent numbers from -256 to 255.

The first bit as a negative is just how it's defined - it's the "data type" in higher level languages, making it able to represent integers.
EDIT: This is why 1000 0000 is -128, as the highest order bit (the first one) when representing integers in two's complement is always negative.

Using 8 bits however, (again, in two's complement) limits the numbers you can add/subtract etc to between -128 to 127, eg 127 + 3 can't be performed with 8 bits as it's outside the available range, and would cause a overflow error.

The sign/modulus system, as described again by the sammy d book, is where an extra bit is added in front of the number to represent the sign. The rest of the digits represent the magnitude, eg 1000 001 has a sign of "-" and a modulusu of "1", meaning it represents -1.

The book doesn't go into detail about how to add/subtract using sign and modulus - only saying "...the sign and the number is not an integral part of the number. This means calculations must take account [sic] of the sign bit separately to the actual calculation" and says nothing further.

However, when performing subtraction, it "recommends" using two's complement on the second binary number to convert it to an addition, eg 0101 0111 - 0001 1100 --> 0101 0111 + 1110 0100. There is no mention of sign/modulus representation here, so presumably you discard the sign/modulus method as a less effective method...

Edit: For clarity, when forming a number in two's complement, draw a table. The below is from the Samuel Davis textbook, it is not my intellectual property, I am not the author, I am reproducing it for educational purposes. XD. THIS IS FOR TWO'S COMPLEMENT!!

Using the numbers from the example above (and other numbers):

-128 64 32 16 8 4 2 1 Decimal
0 0 0 0 0 1 1 0 6
1 0 0 0 1 1 1 0 -114
1 0 0 0 0 0 0 0 -128
1 0 1 0 0 0 0 1 - 95
 
Last edited:

Excalibur_

Not an Executive Member
Joined
Nov 21, 2005
Messages
567
Gender
Male
HSC
2006
In 2's complement, the highest order bit is given a negative 'weighting'. EG. For a 4-bit 2's C number:

0 1 1 0
= 0 x -2^3 + 1 x 2^2 + 1 x 2^1 + 0 x 2^0

This is why when you have 0111 1111 = 127 and 1000 0000 = -128

To the OP -
(0 x -2^7 + 1 x 2^6 ...etc = 127 vs. 1 x -2^7 + 0 x 2^6 ...etc = -128)

A note is that the numbers in 2's complment are not in lexical order, that is it doesn't go logically from -2, -1, 0, 1, 2 etc.

To the OP - I get the feeling that your teacher was mixing up signed integer representation with 2's complement representation. This is where the first bit is reserved purely for the sign (this philosophy is used for floating point integers). The numbers you can represent in this way is -127 to 127 and you'd also have a positive 0 and a negative 0.

A way you can quickly approach subtracting two 2's complement number is so (I'll use a 4 bit example):

0100 - 1110
4 - (-2)
Invert the 2nd operand (1110) and add 1 - this is a quick method of giving 1's complement. Then add the two numbers.

0100 + 0010 = 0110 = 6

If you see any number that calls you to subtract the lowest possible number in a given precision (ie, 1000 in 4-bit such that 0100 - 1000) then it's a guaranteed overflow :D

There's a very quick way of forming a 2c's number from your unsigned binary numbers. Say you want to find -5 in 8-bit 2's C.

5 in 8-bit is 0000 0101
You invert:

1111 1010

Then add 1:

1111 1011

That's -5.

EDIT: You can go from negative to positive the same way

-5 = 1111 1011 (2s C)
Invert:

0000 0100

Add 1:

0000 0101 = 5

(I'm not entirely sure *why* you have to add one but I think its because you've inverted the 1 that you added before which may have carried / stayed etc causing your inversion to actually be one less than the true answer when you bit-wise invert?)
 
Last edited:

Pastafarian

New Member
Joined
Jul 2, 2007
Messages
22
Gender
Undisclosed
HSC
2007
I think I get it now, except how -128 can be represented.

1000 0000 (128base 10)

0111 1111 (1's comp)

1000 0000 (twos comp)

So when calculating the complements you ignore the eigth bit representing the sign? The why isn't it 128 to -128??
 

Excalibur_

Not an Executive Member
Joined
Nov 21, 2005
Messages
567
Gender
Male
HSC
2006
Because the first bit is negatively weighted, 1000 0000 (2c) = -128 but 0111 1111 (2c) = 127.

Consider 4-bit numbers:

0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
----
1000 = -8
1001 = -7
1010 = -6
1011 = -5
1100 = -4
1101 = -3
1110 = -2
1111 = -1

That's why I mean by lexical order, they don't necessarily follow in 'numerical' order.

Also: for a 4-bit number, you can represent 2^4 (16) decimal numbers. 0 to 7 is 8 numbers and -8 to -1 is 8 numbers and 8 + 8 = 16.

Don't forget 0 is a number too...so one end has to be chopped off by a number. In two's complement representation, the positive number has been chopped. There is another representation used to deal with power numbers in floating point called 'excess-k' which is in lexical order, but chops the negative number. (I don't think you do those).

It's just purely how the number representation scheme was conceived, basically. The only hard and fast rule is that you can only represent 2^n numbers given an n-bit binary number.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top