# How to convert positive number to negative number in Two's complement code?

#### Osvaldon

Joined Sep 24, 2016
5
I have two numbers in Two's complement code A and B, where both of them are positive.
I need to subtract B from A ( A - B).
How can I do this when I can do the following:
Perform shifts, invert, +1, -1, SUM, XOR.

I think I should convert B from a positive number to a negative and them sum A and B, but I got not idea how to do that.

#### dl324

Joined Mar 30, 2015
11,543
Welcome to AAC!

Is this homework?

#### Osvaldon

Joined Sep 24, 2016
5
Welcome to AAC!

Is this homework?
Hello, yes it is. I'm supposed to create a micro command for the CPU (or something like that) that would do the following A - B / C^2. I got division and multiplication covered, but I got no idea how to convert B to a negative number so I could A + B, so I'm basically stuck because of it.

#### dl324

Joined Mar 30, 2015
11,543
but I got no idea how to convert B to a negative
What is the relationship between a number and it's two's complement?

#### Osvaldon

Joined Sep 24, 2016
5
What is the relationship between a number and it's two's complement?
Do you mean how to transform a number into two's complement? I think it's inversion then +1 and you got a number in it's two's complement.

What if I were to subtract 1 from B, then do a logical shift left and then right and finally add +1 again, would that work?

#### dl324

Joined Mar 30, 2015
11,543
Do you mean how to transform a number into two's complement? I think it's inversion then +1 and you got a number in it's two's complement.
What is the relationship between a number and it's two's complement? For example 3 and it's two's complement.
What if I were to subtract 1 from B, then do a logical shift left and then right and finally add +1 again, would that work?
This is homework. You tell me.

#### WBahn

Joined Mar 31, 2012
26,156
MOD NOTE: Moved to Homework Help.

#### WBahn

Joined Mar 31, 2012
26,156
Do you mean how to transform a number into two's complement? I think it's inversion then +1 and you got a number in it's two's complement.
You are making a common mistake in your thinking. You do not do this to transform a number into two's complement -- you do this to find the representation of that number's complement (i.e., it's additive inverse) as represented in two's complement. This is conceptually a very different thing.

If I have a value, A, and I have the representation of that value in two's complement, then inverting all the bits and adding one gets me the two's complement representation of -A; and it doesn't matter whether A is positive or negative.

#### WBahn

Joined Mar 31, 2012
26,156
Hello, yes it is. I'm supposed to create a micro command for the CPU (or something like that) that would do the following A - B / C^2. I got division and multiplication covered, but I got no idea how to convert B to a negative number so I could A + B, so I'm basically stuck because of it.
But why do you need to do A + B at all?

Your equation has you finding the square of C. Then dividing B by the square of C. Then subtracting the result of B divided by the square of C from A.

Perhaps you need to review order of operations?

#### WBahn

Joined Mar 31, 2012
26,156
I have two numbers in Two's complement code A and B, where both of them are positive.
I need to subtract B from A ( A - B).
How can I do this when I can do the following:
Perform shifts, invert, +1, -1, SUM, XOR.

I think I should convert B from a positive number to a negative and them sum A and B, but I got not idea how to do that.
So what you are saying is that you think you should perform

A + (-B)

Sounds reasonable.

So how do you take the negative of a two's complement number?

#### Osvaldon

Joined Sep 24, 2016
5
But why do you need to do A + B at all?
My mistake. It's (A - B) / C^2 not A - B / C^2

So what you are saying is that you think you should perform

A + (-B)

Sounds reasonable.

So how do you take the negative of a two's complement number?

So if I take a binary number, invert it and add one, I get it's represantation in two's complement, then if I repeat the same action I get a represantation of that same, but negative number in two's complement?

#### MrChips

Joined Oct 2, 2009
21,877
My mistake. It's (A - B) / C^2 not A - B / C^2

So if I take a binary number, invert it and add one, I get it's represantation in two's complement, then if I repeat the same action I get a represantation of that same, but negative number in two's complement?
No.
Two's complement is a number representation, it is a system for representing positive and negative integer values.

If you have a positive number, its two's complement will be the representation of the negative value.
Forming the twos complement of a negative number will give you the positive value.

• Osvaldon

#### WBahn

Joined Mar 31, 2012
26,156
My mistake. It's (A - B) / C^2 not A - B / C^2

So if I take a binary number, invert it and add one, I get it's represantation in two's complement, then if I repeat the same action I get a represantation of that same, but negative number in two's complement?
No. If you invert it and add one, you get the representation of the additive inverse of that number. If you invert it and add one again, you get the original representation back.

If you have a positive number, then the binary representation of that number is the same either as an unsigned binary representation or as a two's complement representation.

• Osvaldon

#### Osvaldon

Joined Sep 24, 2016
5
No.
Two's complement is a number representation, it is a system for representing positive and negative integer values.

If you have a positive number, its two's complement will be the representation of the negative value.
Forming the twos complement of a negative number will give you the positive value.
Ok thanks, that's what I needed.

Free likes for everyone!

#### dl324

Joined Mar 30, 2015
11,543
Ok thanks, that's what I needed.
Post how you implemented A-B so we'll know you got it.

• Osvaldon