位运算问题
是否有一个位运算或一系列位运算可以给我以下结果?
我将通过示例来展示我想要的内容。请注意,每个位串的长度是无关紧要的:
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
因此,我们的想法是,如果第一个字符串中的任何位置,1 都会与第二个字符串中的 1 重叠,那么结果中,除了1重叠的位置之外,到处都出现1。
Is there a bit operation or a series of bit operations 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
伪代码:
Pseudo code:
您可以使用按位与非,即 按位 AND 求反以获得除情况 2 之外的所有内容、3 和 7。
如果你绝对必须有这两种情况,你可以这样做
。但是,如果你这样做,你必须意识到你无法区分情况 2/3/7 和情况 8。
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
If you do this, however, you must realize that you have no way of telling case 2/3/7 from case 8.
IIRC,一种可靠的方法来进行这种转换,即从您拥有的运算结果数据到数字逻辑,称为“卡诺图”,即您从称为“真值表”的数据开始,并以必要的数字逻辑结束。当然,据此,您可以根据特定于语言的按位运算符/约定转换为任何编程语言。
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.
您是否需要对整个位列表进行一次操作,或者您可以一对一地迭代单个位吗?如果是这样,那就很简单了,如果不是,我相信有一个布尔二元函数可以完全做到这一点(在其中的 16 个函数中)
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)