circuits using karnaugh's map technique - boolean-logic

A combinational circuit is to be designed that counts the number of occurrences of 1 bits in a 4 bit input. However, an input 1111 is an invalid input for the circuit and output in such case will be 00.
One valid input for such circuit may be 1110 with the output 11; another valid input may be 1010 with the output 10.
Draw the truth table for the circuit. Use the Karnaugh map to design the circuit and draw it using AND,OR and NOT gates.

Because the 4bit input can have at most 4 ones in it, we can encode the output into 3bit long binary number.
The truth-table would look like this:
w x y z y_2 y_1 y_0
---------+------------- number of positive bits
0 0 0 0 | 0 0 0 ~ 0
0 0 0 1 | 0 0 1 ~ 1
0 0 1 0 | 0 0 1 ~ 1
0 0 1 1 | 0 1 0 ~ 2
---------+-------------
0 1 0 0 | 0 0 1 ~ 1
0 1 0 1 | 0 1 0 ~ 2
0 1 1 0 | 0 1 0 ~ 2
0 1 1 1 | 0 1 1 ~ 3
---------+-------------
1 0 0 0 | 0 0 1 ~ 1
1 0 0 1 | 0 1 0 ~ 2
1 0 1 0 | 0 1 0 ~ 2
1 0 1 1 | 0 1 1 ~ 3
---------+-------------
1 1 0 0 | 0 1 0 ~ 2
1 1 0 1 | 0 1 1 ~ 3
1 1 1 0 | 0 1 1 ~ 3
1 1 1 1 | 1 0 0 ~ 4
BUT! The output in your case is only on two bits. Your specification also consider the 1111 input as invalid with 00 output. therefore you can simply delete the most significant column in the truth table and there will be no other change:
w x y z y_1 y_0
---------+--------- number of positive bits
0 0 0 0 | 0 0 ~ 0
0 0 0 1 | 0 1 ~ 1
0 0 1 0 | 0 1 ~ 1
0 0 1 1 | 1 0 ~ 2
---------+-------------
0 1 0 0 | 0 1 ~ 1
0 1 0 1 | 1 0 ~ 2
0 1 1 0 | 1 0 ~ 2
0 1 1 1 | 1 1 ~ 3
---------+-------------
1 0 0 0 | 0 1 ~ 1
1 0 0 1 | 1 0 ~ 2
1 0 1 0 | 1 0 ~ 2
1 0 1 1 | 1 1 ~ 3
---------+-------------
1 1 0 0 | 1 0 ~ 2
1 1 0 1 | 1 1 ~ 3
1 1 1 0 | 1 1 ~ 3
1 1 1 1 | 0 0 ~ invalid, showing zeros
Now you can use different styles for minimizing the output functions y_1 and y_0, but I think the Karnaugh maps are suitable for this.
Transfer the lines of the truth-table for each of the output functions into a separate K-map using the indexes (number of line in the table indexed from zero) or comparing the combinations of the variables.
For the output function y_0 the final K-map looks like this and as you can see that is the minimized SOP (DNF; disjunction of conjunctions) function with no larger groups (terms) to be found.
y_0 = ¬w·¬x·¬y·z + ¬w·x·¬y·¬z + ¬w·¬x·y·¬z + ¬w·x·y·z
+ w·¬x·y·z + w·x·y·¬z + w·¬x·¬y·¬z + w·x·¬y·z
For the most significant bit of the output I chose to find the POS (CNF; conjunction of disjunctions), because there are fewer cases of 0 bits than 1 bits in the output.
The output function can be as well as in the y_0 described by marking out all the right bits. In this case it would be this K-map and the function:
y_1 = (w + x + y + z) · (w + x + y + ¬z) · (w + ¬x + y + z)
· (w + x + ¬y + z) · (¬w + ¬x + ¬y + ¬z) · (¬w + x + y + z)
But that can be minimized to this output function in the K-map:
y_1 = (w + x + y) · (w + y + z) · (w + x + z)
· (¬w + ¬x + ¬y + ¬z) · (x + y + z)
After that you can just use the right gates or transform it to more suitable combination of gates using Rott's grids.

Related

Bar chart from many variables where varx = in Stata

I have a bar chart question here. Given that for all the variables in the dataset 1 = yes and 0 = No. I would like to plot a bar graph with the percentages (where var=1) on the y-axis and the variables on the x axis. Thanks in advance.
Dataset
Water
Ice
Fire
Vapor
1
1
0
1
1
0
0
1
0
1
1
1
1
1
1
1
1
1
0
1
1
1
1
0
0
1
1
1
0
1
0
1
0
1
1
1
1
0
1
1
0
1
0
0
0
1
1
0
1
0
1
0
1
0
1
0
1
1
1
1
0
1
0
1
1
0
1
1
1
0
1
0
1
1
0
1
1
0
0
1
0
1
1
1
1
1
0
1
1
0
0
1
0
1
1
1
The percent of 1s in a (0, 1) variable is just the mean multiplied by 100. As you probably want to see the percent as text on the graph, one method is to clone the variables and multiply each by 100.
You could then use graph bar directly as it defaults to showing means. I don't like its default in this case and the code instead uses statplot, which must be installed before you can use it.
* Example generated by -dataex-. For more info, type help dataex
clear
input byte(water ice fire vapor)
1 1 0 1
1 0 0 1
0 1 1 1
1 1 1 1
1 1 0 1
1 1 1 0
0 1 1 1
0 1 0 1
0 1 1 1
1 0 1 1
0 1 0 0
0 1 1 0
1 0 1 0
1 0 1 0
1 1 1 1
0 1 0 1
1 0 1 1
1 0 1 0
1 1 0 1
1 0 0 1
0 1 1 1
1 1 0 1
1 0 0 1
0 1 1 1
end
quietly foreach v of var water-vapor {
clonevar `v'2 = `v'
label var `v'2 "`v'"
replace `v'2 = 100 * `v'
}
* ssc install statplot
statplot *2 , recast(bar) ytitle(%) blabel(bar, format(%2.1f))
Try
. ssc install mylabels
checking mylabels consistency and verifying not already installed...
all files already exist and are up to date.
. sysuse nlsw88, clear
(NLSW, 1988 extract)
. mylabels 0(10)70, myscale(#/100) local(labels)
0 "0" .1 "10" .2 "20" .3 "30" .4 "40" .5 "50" .6 "60" .7 "70"
. graph bar (mean) married collgrad south union, showyvars legend(off) nolabel bargap(20) ylabel(`labels')
. table, statistic(mean married collgrad south union)
------------------------------
Married | .6420303
College graduate | .2368655
Lives in the south | .4194123
Union worker | .2454739
------------------------------
This relies on mylabels, and implements the bar gap (which I also like).

This says its having two 2bit inputs so how to find the truth table

Question 2
A logic circuit is given two 2-bit binary numbers A and B as its inputs. The circuit consists of two outputs Y1 and Y2. The output values of YI and Y2 are obtained as follows:
If A<B, then Y1 and Y2 will be equal to A-B. Else Y1 and Y2 will be equal to A
a) Determine the truth table for the system
b) Obtain the simplified:
i SOP for Y1 and Y2.
ii. POS expressions for YI
Truth Table:
A1 A2 A B1 B2 B A<B? Y1=Y2=A-B||A Y11-Y21 Y12=Y22 Y13=Y23
0 0 0 0 0 0 n 0 0 0 0
0 0 0 0 1 1 y -1 1 1 1
0 0 0 1 0 -1 n 0 0 0 0
0 0 0 1 1 -2 n 0 0 0 0
0 1 1 0 0 0 n 1 0 0 1
0 1 1 0 1 1 n 1 0 0 1
0 1 1 1 0 -1 n 1 0 0 1
0 1 1 1 1 -2 n 1 0 0 1
1 0 -1 0 0 0 y -1 1 1 1
1 0 -1 0 1 1 y -2 1 1 0
1 0 -1 1 0 -1 n -1 1 1 1
1 0 -1 1 1 -2 n -1 1 1 1
1 1 -2 0 0 0 y -2 1 1 0
1 1 -2 0 1 1 y -3 1 0 1
1 1 -2 1 0 -1 y -1 1 1 1
1 1 -2 1 1 -2 n -2 1 1 0
Y11-Y21 K-MAP (Karnaugh map):
A1A2\B1B2 0 0 0 1 1 1 1 0
0 0 0 1 0 0
0 1 0 0 0 0
1 0 1 1 1 1
1 1 1 1 1 1
Y11-Y21 Sum Of Products:
A1 V (A1' ^ A2' ^ B1' ^ B2)
Y11-Y21 Product Of Sums:
(A1 v A2')^(B1' v A1)^(A1 v B1 v B2)
The rest is left was an exercise or for another Answerer.

Why bitshift when evaluating truth tables as binary numbers?

The last answer in this question shows that a binary truth table can be represented as a binary number:
0 0 0 | 1
0 0 1 | 1
0 1 0 | 0
0 1 1 | 0
1 0 0 | 1
1 0 1 | 0
1 1 0 | 1
1 1 1 | 0
Can be represented by 01010011.
The entries in the table can also be evaluated using this number.
def evaluate(f, x):
return (f & (1<<x)) != 0
f = int('01010011',2)
>>> evaluate(f,int('100',2))
True
>>> evaluate(f,int('101',2))
False
My question is about the evaluate function provided by the answer. Why must we left shift the bits by one?
You have it backwards. It is the binary number 1 shifted left by x spots.
And that should make sense. If you want to check the 4th spot in the table, represented by f, you have to check that f & 10000!=0.
You're right, shifting by one is very arbitrary and does not make sense.

Boolean equation from circuit

Could someone please help me understand what the boolean equation performed by this circuit would be?
Label the output of the first mux X.
Then, create a truth table for X, then Y:
C D X A Y
---------
0 0 1 0 1
0 1 0 0 0
1 0 0 0 0
1 1 1 0 1
0 0 1 1 1
0 1 0 1 1
1 0 0 1 1
1 1 1 1 1
From inspection of the truth table:
Y = A + CD + C'D'

In a computer why does adding one to the maximum integer results in zero?

I wonder the process of addition in computers.
How do computers add two number? Is it an electronic magic? Is it an algorithm?
Also, I wonder why adding 1 to Max integer (...111) results in zero (...000)? Asuming ...111 is the binary representation of max int in that computer architecture.
Thank you.
It doesn't always result in 0. If you're using a signed integer type in the most representation, you'll end up with the minimum value, going from (say)
01111111 (127)
10000000 (-127)
On some architectures - and in some modes, at least - this sort of overflow doesn't give a value, it causes an error. When it does give a value, it's basically still just binary counting. Imagine you have an 8-bit unsigned integer value... the last few values would be
11111100
11111101
11111110
11111111
then the logical next step is to go into the next bit:
100000000
So the computer effectively does that - but then throws away that new top bit, keeping just the bottom 8 bits (because it's an 8-bit type).
You don't have to use computers to see this sort of effect though. Suppose you have an analogue odometer in a car - the kind with several "wheels". As you get to the end of its range, it just wraps round:
99997
99998
99999
00000 // Overflow!
This is the case in an arithmetic processor which implements two's complement binary arithmetic for integers. It is not true of, say, floating point numbers and it's not necessarily true on rare computers that do math in other ways (sign-magnitude representations, etc).
two's complement makes certain things easy for the hardware, like implementing some signed and unsigned operations using the same logic.
Remember, the value 1111...1111 represents -1 under two's complement, and you want -1 to increment to 0.
1111111..111 goes to zero "naturally". When you add 1, a carry of 1 propagates through the number, causing every digit to turn to 0, and then it carries out of the number (where it is discarded, or used to set an overflow indication).
The process of addition is hardware related and done ALU in your CPU .
Adding 1 to Max integer will cause in zero
Because in binary system adding 1 to 1 will result in zero and 1 added to the next digit
So the adding 1 to 11111
1+1=0 and 1 shifted to the next digit and so on
How do computers add two number? Is it an electronic magic? Is it an algorithm?
Computers use an algorithm called 2's complement to represent positive and negative numbers (signed numbers).
Following is the explanation of Sign Bit, 1's complement and 2's complement.
Let's take a 4 bit integer
Sign Bit
| Binary | Decimal |
| :-----: | :-----: |
| 1 1 1 1 | -7 |
| 1 1 1 0 | -6 |
| 1 1 0 1 | -5 |
| 1 1 0 0 | -4 |
| 1 0 1 1 | -3 |
| 1 0 1 0 | -2 |
| 1 0 0 1 | -1 |
| 1 0 0 0 | -0 |
| 0 0 0 0 | +0 |
| 0 0 0 1 | +1 |
| 0 0 1 0 | +2 |
| 0 0 1 1 | +3 |
| 0 1 0 0 | +4 |
| 0 1 0 1 | +5 |
| 0 1 1 0 | +6 |
| 0 1 1 1 | +7 |
As you can see from the above truth table, the difference between +5 and -5 is just the left most bit, i.e., the Most Significant Bit.
The MSB is used to determine the sign of the number. Hence, the name.
There are a few problems with this approach,
There's a +0 and a -0.
If you add 2 numbers. For e.g., +5 and -5 you should get 0 but when adding 0 1 0 1 with 1 1 0 1 you get 1 0 0 1 0, we're using 4 bit integer, so we can ignore the first 1. So, we get 0 0 1 0 which is 2.
So, to fix the issues, scientist devised 1's complement.
1's Complement
| Binary | Decimal |
| :-----: | :-----: |
| 1 0 0 0 | -7 |
| 1 0 0 1 | -6 |
| 1 0 1 0 | -5 |
| 1 0 1 1 | -4 |
| 1 1 0 0 | -3 |
| 1 1 0 1 | -2 |
| 1 1 1 0 | -1 |
| 1 1 1 1 | -0 |
| 0 0 0 0 | +0 |
| 0 0 0 1 | +1 |
| 0 0 1 0 | +2 |
| 0 0 1 1 | +3 |
| 0 1 0 0 | +4 |
| 0 1 0 1 | +5 |
| 0 1 1 0 | +6 |
| 0 1 1 1 | +7 |
In this, everything from +0 to +7 is the same as before. The difference is with the negative numbers. We take the positive number's binary, and we complement all the bits. So, 0 becomes 1 and 1 becomes 0.
You can see for e.g., +5 and -5, the former is 0 1 0 1 and the latter is 1 0 1 0.
So, now if we add +5 and -5 like before 0 1 0 1 and 1 0 1 0, it gives 1 1 1 1 which is -0.
But here also, we have some issues,
There still is +0 and -0.
Let's try adding +5 and -3 that's 0 1 0 1 and 1 1 0 0, adding them we get, 1 0 0 0 1. Again, ignoring the first 1, we get 0 0 0 1
and that's +1 but it should be +2. Similarly, if we add +6 and -2 we should get +4 but adding 0 1 1 0 and 1 1 0 1, we get 1 0 0 1 1 i.e., 0 0 1 1 which is +3.
So, if we add +1 to all the answers, the answers becomes correct. 0 0 1 1 + 1 is 0 1 0 0 which is +4.
2's Complement
This method is very similar to 1's complement.
| Binary | Decimal |
| :------: | :----------: |
| -8 4 2 1 | place values |
| 1 0 0 0 | -8 |
| 1 0 0 1 | -7 |
| 1 0 1 0 | -6 |
| 1 0 1 1 | -5 |
| 1 1 0 0 | -4 |
| 1 1 0 1 | -3 |
| 1 1 1 0 | -2 |
| 1 1 1 1 | -1 |
| 0 0 0 0 | +0 |
| 0 0 0 1 | +1 |
| 0 0 1 0 | +2 |
| 0 0 1 1 | +3 |
| 0 1 0 0 | +4 |
| 0 1 0 1 | +5 |
| 0 1 1 0 | +6 |
| 0 1 1 1 | +7 |
What this method does is, it gets rid of the -0 and therefore, we have -8. So, 0 1 1 1 1 1 1 1 i.e., 127 becomes 1 0 0 0 0 0 0 0 i.e., 127 -128 because 0 to 127 is 128 and -1 to -128 is 128. Hence, 2^8 = 256.
And, if we add 2 numbers just like before +5 and -5 i.e., 0 1 0 1 and 1 0 1 1 we get 1 0 0 0 0 i.e., 0 0 0 0 which is exactly 0. No weird -0 and +0. Let's take another example i.e +6 and -2 i.e., 0 1 1 0 and 1 1 1 0, we get 1 0 1 0 0 i.e., 0 1 0 0 which is 4. YAY!.
But that's not all, 2's complement packs even more logic. In the above table you can see the Least significant bit i.e., the right most bit is 1 i.e., 2^0 and then 2 i.e., 2^1 and then 4 i.e., 2^2 but then -8 i.e., -2^3. So, if we take -5 as example, we get 1 0 1 1 i.e., (1 * -2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 2^0) i.e., -8 + 0 + 2 + 1 which is -5. Maths is the real magic here.
Now, to negate the number +5 for example. We have to do 2 steps, instead of 1 like the previous methods.
We have to take complement (invert) i.e., 0 1 0 1 becomes 1 0 1 0.
Then, we have to add 1 i.e., 1 0 1 0 + 1 so, it becomes 1 0 1 1 which is -5.
This method works great, and we get an extra number to store. That's the reason a normal int (32 bit) in C++ has a range of 2^31 to 2^31 - 1.