boolean algebra - build a OR gate as an NAND gate - boolean-logic

I am trying to wrap my mind around how to do this.
For what i understand is that a set of logic gates is called "functionally complete" if some combination of the gates can be used to do each of the basic logic operations AND, OR, and NOT. The claim is the NAND gate is functionally complete.
What i dont understand is how to build a OR gate as a nand gate. build a AND gate from a NAND gate etc.. would the formula i come up with have to have the same output?
X' = X NAND 1
X + Y = ?
X * Y = ?
using a truth table how is X' = X NAND 1?
I am not sure what X NAND 1 means.. I understand 1 is fixed as y?
I get confused when i see the gate inbetween 2 inputs like x NAND y
How can i construct a truth table for x+y = NAND?
or should i do it a different way?

Just go by definition:
X NAND Y = ~ (X AND Y) = ~X OR ~Y
Substitute Y = 1 and see you will get
X NAND 1 = ~X OR ~1 = ~X OR 0 = ~X = X'
Edit:
Just so that you get a sense on how to build other gates using NAND gate, this wikipedia article is very good and informative. Hope it helps.
http://en.wikipedia.org/wiki/NAND_logic

Yes, X NAND 1 is like X NAND Y with Y fixed as 1. The thing you're comparing X with doesn't have to be called Y; it can be any variable, any constant or the result of another comparison. All that matters is whether the value is a 0 or a 1, in the end.
Example:
X | Y | 1 | X OR Y
---+---+---+--------
0 | 0 | 1 | 0
0 | 1 | 1 | 1
1 | 0 | 1 | 1
1 | 1 | 1 | 1
Now you could do X AND Y, X AND 1 or X AND (X OR Y) just by comparing the numbers in the first column with numbers in the second, third or fourth columns, respectively.
As for NAND specifically, just remember that it means the opposite of AND. It actually stands for "not and." So if you ANDed two things together and got 0, then NANDing the same two things together would give you 1.
That said, your last question doesn't make much sense. There's no such thing as X+Y = NAND. X, Y and X+Y are values; NAND is a gate. You can't compare numbers to gates. Your question is asking you to use NAND gates to compare things over and over until you you get a column of zeroes and ones that looks the same as X+Y does.
EDIT:
Okay, let's look at your question "using a truth table how is X' = X NAND 1?"
X | X' | 1 | X AND 1 | X NAND 1 is the same as the opposite of X AND 1
---+----+---+-------------+-------------------------------------------------
0 | 1 | 1 | 0 AND 1 = 0 | 1 (opposite of 0)
0 | 1 | 1 | 0 AND 1 = 0 | 1 (opposite of 0)
1 | 0 | 1 | 1 AND 1 = 1 | 0 (opposite of 1)
1 | 0 | 1 | 1 AND 1 = 1 | 0 (opposite of 1)
And looking at each column, we can see that X' has the same values as X NAND 1

NAND is basically the reverse of AND:
Truth Table
A B A NAND B A AND B A OR B A NOR B
0 0 1 0 o 1
0 1 1 0 1 0
1 0 1 0 1 0
1 1 0 1 1 0
By making the right combinations using these and the remaining boolean operators you should be able to construct any one in terms of the others

Quick truth tables:
NAND 1 0
0 1 1
1 0 1
OR 1 0
0 1 0
1 1 1
NOT
1 0
0 1
What functionally complete means is that, given a pile of the complete gate, you can construct any other gate type.
So if you build a circuit with 1 NAND gate, you get exactly the opposite of an OR gate (inputs reversed). If your goal is to build the OR gate, you have to invert the inputs of the NAND gate. That's easy to do with a couple NOT gates (which is, if you look carefully, the same as a NAND gate with one of its inputs tied to logical 1). So you put those NOT gates before your NAND gate and voila, an OR gate falls out.
For your confusion, putting the gate between its two inputs is just using that gate as a binary operator, like a + sign. It's the same as saying NAND(X, 1) or "The output of the NAND gate when its inputs are X and 1."

Related

Is it possible to change only some part of decimal in mysql?

I know it is kind of foolish question but is it possible to change only integer part of decimal value.e.g suppose I have number 12.34 then I want to change only 12 and .34 remain as it is.
I have two tables. table X and table Y
Table X
id(int)| value(decimal)
-------|--------
1 | 12.43
2 | 3.54
3 |102.07
Table Y
id(int)| value(int)
-------|--------
1 | 32
2 | 76
3 | 8
I want to resultant table x like below
Table X
id | value
------|--------
1 | 32.43
2 | 76.54
3 | 8.07
Replace int part of value of table X with value of table Y.
Is it possible in mysql?(with out any function call, something like string replace).
To isolate the decimal portion of the values in the X table you may use the following expression:
X.value - FLOOR(X.value)
In other words, we can subtract off the whole number component.
SELECT
x.id,
y.value + x.value - FLOOR(x.value) AS value
FROM X x
INNER JOIN Y y
ON x.id = y.id;
Demo

MySQL query finding max between columns where non-zero

Suppose I have the following data:
data
Names X1 X2 X3
Jimmy 1 3 0
Mark 2 0 0
Jimmy 4 0 5
I am having trouble writing a unique query. What I would like return is the last number for each row that is non-zero. So the result would look like
Names Want
Jimmy 3
Mark 2
Jimmy 5
Is there a way to do this?
If you have a finite number of columns, you can use a CASE statement:
SELECT name,
CASE WHEN x3 <> 0 THEN x3
WHEN x2 <> 0 THEN x2
ELSE x1 END AS want
FROM myTable;
Here is an SQL Fiddle example.

KDB: apply dyadic function across two lists

Consider a function F[x;y] that generates a table. I also have two lists; xList:[x1;x2;x3] and yList:[y1;y2;y3]. What is the best way to do a simple comma join of F[x1;y1],F[x1;y2],F[x1;y3],F[x2;y1],..., thereby producing one large table?
You have asked for the cross product of your argument lists, so the correct answer is
raze F ./: xList cross yList
Depending on what you are doing, you might want to look into having your function operate on the entire list of x and the entire list of y and return a table, rather than on each pair and then return a list of tables which has to get razed. The performance impact can be considerable, for example see below
q)g:{x?y} //your core operation
q)//this takes each pair of x,y, performs an operation and returns a table for each
q)//which must then be flattened with raze
q)fm:{flip `x`y`res!(x;y; enlist g[x;y])}
q)//this takes all x, y at once and returns one table
q)f:{flip `x`y`res!(x;y;g'[x;y])}
q)//let's set a seed to compare answers
q)\S 1
q)\ts do[10000;rm:raze fm'[x;y]]
76 2400j
q)\S 1
q)\ts do[10000;r:f[x;y]]
22 2176j
q)rm~r
1b
Setup our example
q)f:{([] total:enlist x+y; x:enlist x; y:enlist y)}
q)x:1 2 3
q)y:4 5 6
Demonstrate F[x1;y1]
q)f[1;4]
total x y
---------
5 1 4
q)f[2;5]
total x y
---------
7 2 5
Use the multi-valent apply operator together with each' to apply to each pair of arguments.
q)raze .'[f;flip (x;y)]
total x y
---------
5 1 4
7 2 5
9 3 6
Another way to achieve it using each-both :
x: 1 2 3
y: 4 5 6
f:{x+y}
f2:{ a:flip x cross y ; f'[a 0;a 1] }
f2[x;y]
5j, 6j, 7j, 6j, 7j, 8j, 7j, 8j, 9j

Method for constructing a gate

I'm trying to construct a multiplex gate. It has two inputs, and one selector. I got as far as
the truth table.
A | B | Sel | Out
0 0 1 0
0 1 1 0
1 0 1 1
1 1 1 1
0 0 0 0
0 1 0 1
1 0 0 0
1 1 0 1
And this is where my method fails. I've constructed simpler gates such as AND, and OR. Those were so simple I didn't need an articulate method. I went to wikipedia to see if I could get
a method. Instead I only discovered which gates I need to construct the circuit. For my goals, this misses the point. More important to me is the method that arrives at the answer, rather than the answer itself. I know I need to use DeMorgan's Laws, but fall down when trying to come up with specifics. Any hints would be most welcome.
Just to elaborate on Keith's answer, here's the Karnaugh map for your truth table:
AB
00 01 11 10
___________
sel 0 | 0 1 1 0
1 | 0 0 1 1
This is created by grouping A and B, and then making a matrix of the outputs for any given input. Note the column headings do not count in binary, rather they are more like a grey code, having only one transition between each column.
Now that's done, you can write an equation that ORs together terms that cover all the 1s in the Karnaugh map.
On the Karnaugh map, it's pretty easy to see terms that cover multiple 1s. For example, the term B.sel' (B and not sel) covers both the 1's in the top row.
That combined with A.sel for the 1's in the bottom row gives the equation
output = B.sel' + A.sel
This works out at 4 gates, including the NOT.
You can make a Karnaugh Map, which will help you pick the gates you need to implement your function.

Define quadrant based on positive/negative values in two columns

I have a data set with two columns of positive and negative numbers. I would like to create a third column that reflects which quadrant they would appear in if plotted in Cartesian space.
For example, if Column A is positive, and Column B is positive, then Column C would record "I." If column A is negative, and Column B is negative, then Column C would record "III," and so on.
I suspect I can do this with an if else function and then loop or apply it across rows in the data set, but my attempts to write the if else have so far failed.
Well, the following would give you values between 1 and 4:
C <- (A<0) + (B<0)*2L + 1L
This transforms the whole column in one go. The magic lies in that FALSE/TRUE is treated as 0/1, and you can do math on it. By using 2L and 1L instead of 2 and 1, you keep the result as integers instead of forcing a coercion to doubles (which is slower and takes more memory).
Then assuming you want to map to these quadrants:
+B
|
II | I
-A -----+---- +A
III | IV
|
-B
You could use this (updated to use a data.frame):
# Sample data.frame with columns a & b
d <- data.frame(a=c(1,-1,-1,1), b=c(1,1,-1,-1))
quadrantNames <- c('I', 'II', 'IV', 'III') # Your labels...
d <- within(d, c <- quadrantNames[(a<0) + (b<0)*2L + 1L])
head(d) # print data
a b c
1 1 1 I
2 -1 1 II
3 -1 -1 III
4 1 -1 IV
...and if you want the quadrants mapped differently, just change the order of the labels in quadrantNames.