Is there a bit operation or a series of bit operation开发者_开发百科s that would give me the following result?
I'll show what I want by using examples. Note that the length of each bit string is irrelevant:
1)
100000
100000
------
011111
2)
000000
000000
------
000000
3)
100000
000000
------
000000
4)
000100
000100
------
111011
5)
100100
100100
------
011011
6)
100100
000100
------
111011
7)
010101
101010
------
000000
8)
111111
111111
------
000000
So, the idea is that if anywhere in the first string, a 1 overlaps with a 1 in the second string, then in the result, 1s appear everywhere except the position where the 1s overlap.
Pseudo code:
if (a & b)
return ~(a & b)
else
return 0
You could use a bitwise nand, that is a bitwise AND negated to get all but case 2, 3 and 7.
If you absolutely must have those two cases you could do
result = a & b; // Bitwise and of the two inputs
if (result != 0) { // If we have no matches, we want it to stay 0.
result = ~result;
}
If you do this, however, you must realize that you have no way of telling case 2/3/7 from case 8.
IIRC, one reliable way to make such conversion i.e. from the operation result data you have to the digital logic is called "Karnaugh Map", i.e. you start with the data, called "Truth Table", and end up with the necessary digital logic. Of course, from that, you can convert into any programming language given the language-specific bit-wise operators/conventions.
do you need a single operation on the whole list of bits or you can iterate on the single bit couple one by one? if so, it's trivial, if not, i believe there is a boolean binary function that does that exactly (among the 16 of them)
精彩评论