4-bit ALU subtraction

Thread Starter

David44

Joined Dec 29, 2016
13
Working on a college assignment,
I have 2 inputs,
input A = 0101
input B = 1111

Looking to perform the operation, A minus B and the function table of the ALU mentions that Cn must equal 1.
There is room for a carry out but not an overflow.

By looking at it I can see the answer is -5. But how can this be represented in a 4-bit output number?

So far I have read to complement B so it becomes 0000, and added this to 0101 and the carry in of 1.
But this results in 0110. Not sure about this so any help is welcomed.
 

WBahn

Joined Mar 31, 2012
29,976
Instead of making us guess, it would be helpful if you told us what number representation you are using. Sure, I can make good guess that it is two's complement, but engineering is not about guessing.

So, assuming that I guessed correctly, what is the range of values that can be represented by a 4-bit two's complement number?

What is the normal procedure for taking the two's complement of a number? How does this relate to what you read about?
 

Thread Starter

David44

Joined Dec 29, 2016
13
Instead of making us guess, it would be helpful if you told us what number representation you are using. Sure, I can make good guess that it is two's complement, but engineering is not about guessing.

So, assuming that I guessed correctly, what is the range of values that can be represented by a 4-bit two's complement number?

What is the normal procedure for taking the two's complement of a number? How does this relate to what you read about?
Thanks for your reply.

I am working blind on this as it is not covered in the course text I have (not sure why they are asking it).

But anyway, the range of values for 4-bit twos complement would be -8 to 7?

I stumbled upon opinions regarding twos complement, am I going about it the wrong way?
 

WBahn

Joined Mar 31, 2012
29,976
Thanks for your reply.

I am working blind on this as it is not covered in the course text I have (not sure why they are asking it).

But anyway, the range of values for 4-bit twos complement would be -8 to 7?

I stumbled upon opinions regarding twos complement, am I going about it the wrong way?
So let's make some assumptions that are pretty safe.

Your ALU contains an adder that treats its inputs as the usual UNSIGNED binary representations.

You are treating the input and output values as two's complement SIGNED binary representations.

Once you are done with this problem, you should have a good idea of WHY two's complement is, by far, the dominate way of representing signed integers.

As you have just indicated, 4-bit two's complement can represent values between -8 and +7. So that means that you CAN represent -5.

Now, how do YOU, on paper, find the two's complement representation for -5? Describe the steps.
 

Thread Starter

David44

Joined Dec 29, 2016
13
Well +5 would be: 0101

Using ones complement gives: 1010

And then adding one for twos complement would give: 1011

Am I on the right track?
 

WBahn

Joined Mar 31, 2012
29,976
Well +5 would be: 0101

Using ones complement gives: 1010

And then adding one for twos complement would give: 1011

Am I on the right track?
Yes, you are.

Now consider what you said the instructions told you to do:

"So far I have read to complement B so it becomes 0000, and added this to 0101 and the carry in of 1."

How does that compare to what you did by hand to find the negative of 5?

Now consider that the goal is to find A - B, but isn't that the same as A + (-B)?

Do you see how all of this ties together?
 

Thread Starter

David44

Joined Dec 29, 2016
13
Yes, you are.

Now consider what you said the instructions told you to do:

"So far I have read to complement B so it becomes 0000, and added this to 0101 and the carry in of 1."

How does that compare to what you did by hand to find the negative of 5?

Now consider that the goal is to find A - B, but isn't that the same as A + (-B)?

Do you see how all of this ties together?
Yes I believe I do see it now.

In my initial attempt I had just performed ones compliment on input B, when I should of included A as well?

It would probably save on time and effort to look at it as A + (-B)
 

WBahn

Joined Mar 31, 2012
29,976
Yes I believe I do see it now.

In my initial attempt I had just performed ones compliment on input B, when I should of included A as well?

It would probably save on time and effort to look at it as A + (-B)
By "included A as well" do you mean take the one's compliment of A? If so, then what is leading you to that conclusion? NO, you don't want to do that.

You want to perform A - B using an adder. So change it to A + (-B) so that you are adding two numbers, A and (-B). You already have A, so there is no need to do anything to it. But you do need to get (-B) somehow. Since it is two's complement, you can negate B by taking the one's complement (which is just inverting all the bits) and adding 1 to it. Well, what is the effect of asserting the Cin input to the adder?
 

Thread Starter

David44

Joined Dec 29, 2016
13
By "included A as well" do you mean take the one's compliment of A? If so, then what is leading you to that conclusion? NO, you don't want to do that.

You want to perform A - B using an adder. So change it to A + (-B) so that you are adding two numbers, A and (-B). You already have A, so there is no need to do anything to it. But you do need to get (-B) somehow. Since it is two's complement, you can negate B by taking the one's complement (which is just inverting all the bits) and adding 1 to it. Well, what is the effect of asserting the Cin input to the adder?
So are you saying once i have performed twos complement on B, which would be 0001.
And then adding it to A and the carry in of 1?

sorry if i am going around in circles, i am a bit slow on the ball tonight
 

WBahn

Joined Mar 31, 2012
29,976
So are you saying once i have performed twos complement on B, which would be 0001.
And then adding it to A and the carry in of 1?

sorry if i am going around in circles, i am a bit slow on the ball tonight
Close. You take the negative of B by taking the one's complement of B and then adding 1, right?

Well, flipping all of the bits in B is taking the one's complement of B, right?

Now you just need to add 1. Well, what does asserting the Carry-in bit do if not add 1?
 

Thread Starter

David44

Joined Dec 29, 2016
13
Close. You take the negative of B by taking the one's complement of B and then adding 1, right?

Well, flipping all of the bits in B is taking the one's complement of B, right?

Now you just need to add 1. Well, what does asserting the Carry-in bit do if not add 1?
Sorry I don't follow. Does the carry in have any effect if adding 0101 + 0001?
Would the answer still not be 0110?
 

WBahn

Joined Mar 31, 2012
29,976
Sorry I don't follow. Does the carry in have any effect if adding 0101 + 0001?
Would the answer still not be 0110?
It appears that the problem is that you don't know what an adder does; so it isn't much of a surprise that you are having trouble with seeing how you can implement subtraction with one.

An adder implements the following function:

Sout = (A + B + Cin) mod 2^N
Cout = (A + B + Cin) ≥ 2^N

where N is the number of bits in the input.

For our purposes, it is just important to note that Cin is merely added to the result of A+B. So if Cin is asserted and B is inverted, we have

(A + ~B + 1)

But (~B + 1) is the two's complement negation of B, so we have

(A + (-B)) = A - B

If we have

A = 0101 (=5)
B = 1111 (=-1)

If we take A - B, we want the result to be 5 - (-1) = 6 (0110)

Gee, that looks an awful lot like the answer you got.
 
Top