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

Thread Starter

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.
 

Thread Starter

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.
 

Thread Starter

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
9,329
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
24,971
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
24,971
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
24,971
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?
 

Thread Starter

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?
Invert B and add one?

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
19,742
My mistake. It's (A - B) / C^2 not A - B / C^2



Invert B and add one?

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.
 

WBahn

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



Invert B and add one?

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.
 

Thread Starter

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!
 
Top