# Problem in 2's compliment

13 messages
Open this post in threaded view
|

## Problem in 2's compliment

 Here is my snippet,         Not16(in=x, out=notx);         Inc16(in=notx, out=out); Ideally it should give me a 2's compliment , but I am getting a zero, I am not sure why? I am using built in ones.
Open this post in threaded view
|

## Re: Problem in 2's compliment

 Administrator I thought I had replied to this the day you posted it. Apparently not. My apologies. I need to see more of your code to have a chance of seeing what is going on. When you say you are getting zero, do you mean that regardless of what the value of x is that you are getting a value of zero for out? What are you using this for? You don't need anything like this for the ALU.
Open this post in threaded view
|

## Re: Problem in 2's compliment

Open this post in threaded view
|

## Re: Problem in 2's compliment

 Administrator I don't understand what you mean by "any value of zero" or "like till not operation". If you set x=0 and pass that through the Not gate, you will get all 1's (which is the two's complement representation of -1). If you then increment this, you will get all 0's (since the leading one is lost). But this is what you should get, since -(0) = 0. But you don't need to implement arithmetic negation this way. Your ALU can selectively invert any combination of the inputs and outputs, right? Look at the table of ALU control signals and the operations that result. One of those operations is -x and another is -y. Look at the effect of putting the control signals in those states and walk through what happens to your signal from input to output.
Open this post in threaded view
|

## Re: Problem in 2's compliment

 Administrator Also, if you just added these to the rest of the ALU operation, then I'm guessing you have more than one part driving the 'out' signal, which is not allowed.
Open this post in threaded view
|

## Re: Problem in 2's compliment

 Pardon me, I meant for any value I enter , I get a 0. I enter a value, it goes in the not16 gate, gives me the expected value, say x=10, I put it in not16 gate, I get -11. Then I put the output of the not16 gate to a inc16, which yeilds a zero, for the said input, I am not sure why. On Sat, May 29, 2021, 1:06 AM WBahn [via Nand2Tetris Questions and Answers Forum] <[hidden email]> wrote: Also, if you just added these to the rest of the ALU operation, then I'm guessing you have more than one part driving the 'out' signal, which is not allowed. If you reply to this email, your message will be added to the discussion below: http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Problem-in-2-s-compliment-tp4036032p4036040.html To unsubscribe from Problem in 2's compliment, click here. NAML
Open this post in threaded view
|

## Re: Problem in 2's compliment

 Administrator Are ANY other parts in your ALU tied to the 'out' signal? What happens if you change the name of the signal at the output of your Inc16 to something else and look at it? Feel free to post your entire code. We can delete it once we are finished discussing it and figuring out what is happening.
Open this post in threaded view
|

## Re: Problem in 2's compliment

 Here is my entire code: CHIP ALU {     IN           x[16], y[16],  // 16-bit inputs                 zx, // zero the x input?         nx, // negate the x input?         zy, // zero the y input?         ny, // negate the y input?         f,  // compute out = x + y (if 1) or x & y (if 0)         no; // negate the out output?     OUT         out[16], // 16-bit output         zr, // 1 if (out == 0), 0 otherwise         ng; // 1 if (out < 0),  0 otherwise     PARTS:         Not16(in=x, out=notx);         Inc16(in=notx, out=out);    // Put you code here: } I am only handling the input and output as of now in this case, so I take in x, do the "not", then do the "inc", which according to my understanding should be a 2's compliment, here is my output with one of the vaues:
Open this post in threaded view
|

## Re: Problem in 2's compliment

 Administrator I don't see anything wrong. Are you stepping the simulator after you enter the value for 'x'? I'm guessing that you are, since your Time is showing as 1 and I think you have to do this to get the internal pins updated. Are you POSITIVE that you are using the built-in chips? Look in the directory that your ALU.hdl is in and be certain that there is no Inc16.hdl in there.
Open this post in threaded view
|

## Re: Problem in 2's compliment

 My bad, I thought I had deleted the chips, but I had deleted the test file of inc16 instead of the HDL file, it seems to work fine after that, thanks a bunch
Open this post in threaded view
|

## Re: Problem in 2's compliment

 Administrator Good. Glad we got that figured out. Now, be sure that you understand why you don't need to include a circuit to take the two's complement additive-inverse. It will make your ALU implementation a LOT simpler.