how to translate statement to logic statement?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

how to translate statement to logic statement?

Cifer
I found it was really hard for me to translate

out = 1 if (a == 1 and b == 1)
       0 otherwise

to
 
Not(Nand(a,b))

Anything can help me to do that ?

PS: It seems I lack of some knowledge between the two steps.
Reply | Threaded
Open this post in threaded view
|

Re: how to translate statement to logic statement?

ivant
You should start thinking of hardware a bit different. For example, in software:
if (cond)
  doSomething();
else
  doSoemthingElse();
you test cond and depending on its value you execute either doSomething() or doSomethingElse(). In hardware, you basically execute both at the same time and then choose which which answer to propagate and which to ignore.

You may also find it easier to visualize this using gates instead of HDL. A good tool for this is logisim.

And when you need to write it back in HDL, the HDL Survival Guide is indispensable.
Reply | Threaded
Open this post in threaded view
|

Re: how to translate statement to logic statement?

WBahn
Administrator
In reply to this post by Cifer
When you are first starting out with this stuff, going to the basics is often a good way to go. With practice and experience you will quickly get to the point where these things are pretty obvious.

The basics in digital logic comes down to truth tables (for combinatorial logic) and state diagrams (for sequential logic).

For this problem, set up a truth table for each possible value of your inputs and the corresponding output.

And gate
a b out
0 0  0
0 1  0
1 0  0
1 1  1

Now loot at your available logic gates and see if you can produce that output using just those gates. I'm guessing at the point you are doing this you only have the Nand gate and the Not gate.

Nand gate
a b out
0 0  1
0 1  1
1 0  1
1 1  0

Not gate
in out
0  1
1  0

Now comes the "intuition" part of it. Notice that if you invert the value in the 'out' column for the Nand gate, you get what you are looking for in the 'out' column of the And gate, so running the output of and And gate through a Not gate will yield what you want.

Another way is to think of the names involved -- the names of the gates were not chosen randomly.

Nand = Not And

You should be comfortable with the notion of "double negative" from everyday usage, both in math and in English conversation. In math -(-5) = 5, the two negatives cancel. In conversation saying, "I'm not going to not do something," means that you are going to do something, again the two negatives cancel.

So if we want to get rid of the Not in front of the And in our Nand statement, we simply preceed it with another Not.

And = Not Not And

Since the Not And at the end is a component, we can't break it up, so we surround it by parens to make this explicit.

And = Not (Not And)

And since Not And is the same as Nand, we get

And = Not (Nand)