Why bitshift when evaluating truth tables as binary numbers? - binary

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.

Related

circuits using karnaugh's map technique

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.

Find the matching between multiple tables in database

I have four tables,in each one I have two columns ID and Available. I need a Select Statement that finds the set of ID's that corresponds to available =0.
Table A: ID : 1 2 3 4 5 6
Available :1 1 0 0 0 0
Table B : ID 1 2 3 4 5 6
Available 1 1 1 0 0 0
Table C : ID 1 2 3 4 5 6
Available 0 1 0 0 1 0
Table D : ID 1 2 3 4 5 6
Available 1 1 0 0 1 0
If I understand correctly, you can use intersect if you want all the available values to be 0:
select id from a where available = 0
intersect
select id from b where available = 0
intersect
select id from c where available = 0
intersect
select id from d where available = 0;
If you want any of them to be 0, then use union instead.

Find Average Mark For Each User - Mysql

First table :
UserId UserName
1 User1
2 User2
3 User3
4 User4
Second Table
Userid Mark Aptitude English Technical Status
1 40 1 0 0 S
1 30 0 1 0 F
2 60 0 0 1 S
2 75 0 1 0 F
2 25 0 1 0 F
3 45 1 0 0 F
3 45 1 0 0 D
3 50 0 0 1 F
3 50 0 0 1 F
I have this two table. I need a query to get the each user average mark in English, Aptitude and Technical. The average should be calculated only for status F. The result should be like this
UserId AptitudeAverage EnglishAverage TechnicalAverage
1 0 30 0
2 0 50 0
3 45 0 50
4 0 0 0
Try this:-
SELECT userID, IFNULL(AVG(case when Aptitude = 1 then Mark * Aptitude end), 0) AS AptitudeAverage,
IFNULL(AVG(case when English = 1 then Mark * English end), 0) AS EnglishAverage,
IFNULL(AVG(case when Technical = 1 then Mark * Technical end), 0) AS TechnicalAverage
FROM YOUR_TAB
WHERE Status = 'F'
GROUP BY userID;
This might help you.
Here is the fiddle.
http://sqlfiddle.com/#!9/e449f/21

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.