What is the easiest and informal way to remember XOR and Inclusive-OR logic? - xor

If I find hard to remember XOR and Inclusive-OR, what is the easiest way to remember the logic and truth table?

XOR: One or the other, but not both
OR: One, or the other, or both
One way to think about it is that XOR (eXclusive-OR) is exclusively OR and not AND. Another way is that XOR is exclusive in that you can only pick one of the options: i.e. "You can't have your cake and eat it too."

Inclusive OR :
"false if both values are false"
XOR :"false if both values are same"

Related

Implementation of the switch statement in C [duplicate]

I read somewhere that the switch statement uses "Binary Search" or some sorting techniques to exactly choose the correct case and this increases its performance compared to else-if ladder.
And also if we give the case in order does the switch work faster? is it so? Can you add your valuable suggestions on this?
We discussed here about the same and planned to post as a question.
It's actually up to the compiler how a switch statement is realized in code.
However, my understanding is that when it's suitable (that is, relatively dense cases), a jump table is used.
That would mean that something like:
switch(i) {
case 0: doZero(); break;
case 1: doOne();
case 2: doTwo(); break;
default: doDefault();
}
Would end up getting compiled to something like (horrible pseudo-assembler, but it should be clear, I hope).
load i into REG
compare REG to 2
if greater, jmp to DEFAULT
compare REG to 0
if less jmp to DEFAULT
jmp to table[REG]
data table
ZERO
ONE
TWO
end data
ZERO: call doZero
jmp END
ONE: call doOne
TWO: call doTwo
jmp END
DEFAULT: call doDefault
END:
If that's not the case, there are other possible implementations that allow for some extent of "better than a a sequence of conditionals".
How swtich is implemented depends on what values you have. For values that are close in range, the compiler will generally generate a jump table. If the values are far apart, it will generate a linked branch, using something like a binary search to find the right value.
The order of the switch statements as such doesn't matter, it will do the same thing whether you have the order in ascending, descending or random order - do what makes most sense with regard to what you want to do.
If nothing else, switch is usually a lot easier to read than an if-else sequence.
On some googling I found some interestin link and planned to post as an answer to my question.
http://www.codeproject.com/Articles/100473/Something-You-May-Not-Know-About-the-Switch-Statem
Comments are welcome..
Although it can be implemented as several ways it depends on how the language designer wants to implement it.
One possible efficient way is to use Hash Maps
Map every condition (usually integer) to the corresponding expression to be evaluated followed by a jump statement.
Other solutions also might work as often switch has finite conditions but a efficient solution shall be to use Hash map

Bitwise and of subsets of an array

Can anyone give a hint how to approach this problem?
Given an array A. Is there any subset of array A in which if we do AND of all elements of that subset then output should be in power of two.
I've thought of generating a power-set and solving but it will have a very bad complexity (2^n).
Thanks in Advance.
You can look at it from a different perspective: pick a power of two. Can we generate it?
This question is easy to answer. Take all items from the set in which the bit corresponding to the power of two is set. Calculate the AND of all of those. The result must by construction have the bit that we looked for set, but it may or may not have any other bits set. If it has other bits as well, then choosing some other (smaller - you can't choose any extra items because they don't have the target bit set) subset wouldn't work either, it could only have more wrong bits set because it would have fewer possibilities to unset bits.
Just do that for all possible powers of two, that's only as many as there are bits in the largest integer in the set.

Bit manipulation clarification in C

i'm learning bit manipulation and i am considering a problem and would like some clarification
If given something like 0000111111111111;
and i want 2 functions:
Assuming i am reading the indexes from right to left and indexes start at 1, i.e index 1-12 =1, index 13,14,15,16 = 0
If i want to make it so that i remove one zero starting from the left
am i correct in thinking that a right shift by 1 would achieve what i want?
If i want to add one zero to the left so it becomes 0001111111111111
how do i go about achieving this?
If i want to make it so that i remove one zero starting from the left
am i correct in thinking that a right shift by 1 would achieve what i
want?
Answer: No. If you right-shift, you are changing the number by shifting all bits by one to the right and the least significant bit (the one on the far right) falls off the end:
0000011111111111;
If i want to add one zero to the left so it becomes 0001111111111111
how do i go about achieving this?
Answer: you left-shift and add 1:
0000111111111111; original
0001111111111110; left-shift
0001111111111111; +1

Transform lookup library

I have two 32bit vectors one derived from other by mathematical/logical transform.
Is there a Perl/C library which can look up what kind of transform(Or list of all possible transforms that library is capable of inferring) has been applied ?
Case1. 968eac37 -> 968eac37
Case2. 12345678 -> 23456781
Case3. 614e1973 -> 30f7150d
Output
1. No transform
2. Bit wise shift left 4 or trivial addition
3. Trivial subtraction or something else or unknown
No, this would involve actually applying all the transforms to check the results. Any transform that involved encryption would be by definition not determinable.
Take the case of "trivial subtraction". How would you distinguish that from "trivial addition/multiplication modulo 32 bits"?
Your underlying question is really "how do I undo encryption", which for any sufficiently strong encryption is impossible. For "weak" encryptions there can be multiple answers, so there is no such library.

Variable elimination in Bayes Net

We have studied the Variable Elimination recently and the teacher emphasizes that it is the Bayesian Network that makes varibale elimination more efficient.
I am bit confused on this,why is this the case?
Hope you guys can give me some idea,many thanks.
Robert
Bayesian Networks can take advantage of the order of variable elimination because of the conditional independence assumptions built in.
Specifically, imagine having the joint distribution P(a,b,c,d) and wanting to know the marginal P(a). If you knew nothing about the conditional independence, you could calculate this by summing out over b,c and d. If these have k-ary domains, you need to do O(k^3) operations.
On the other hand, assume that you have a bayes net where A is the root, B is a child of A, C is a child of B and D is a child of C. Then, you can rewrite the joint as P(a|b)P(b|c)P(c|d)P(d) and distribute your three summations as far to the right of the equation as possible. When you actually want to compute P(a), you can precompute the value of sum_d P(d) and store this function. Likewise, you can precompute the value of P(c|d)*sum_d P(d) and store this.
In this way, you end up doing work of O(k^w*+1), where W* is the largest number of children any node in your bayes net has. In this case, we do O(k^2) work, which is also the size of the largest conditional probability table we must keep in memory. Note this is better than our original O(k^3) result, and would be even better if we had more variables.
In short, the conditional independence of a BN allows you to marginalize out variables more efficiently. Another explanation of this can be found at http://www.cs.uiuc.edu/class/sp08/cs440/notes/varElimLec.pdf.
I think it's because a variable which one can eliminate is one which has one and only one variable which is dependent on it. In a Bayes net these would be easy to find because they are nodes with a single child.

Resources