【问题标题】:Understanding bitwise binary combination algorithm理解按位二进制组合算法
【发布时间】:2021-10-28 11:16:13
【问题描述】:

我一直在研究列出布尔值数组所有可能组合的算法。

例如,一个由两个布尔值组成的数组可以有以下组合: [真,真],[真,假],[假,真],[假,假]

我找到了几个使用位运算符的示例,虽然我查看了所用运算符的定义,但我仍然不了解它们在算法中的上下文用途。

我在下面列出了一个示例算法(来源:http://zacg.github.io/blog/2013/08/02/binary-combinations-in-javascript/),并带有注释来描述我在查看它们时看到的内容:

function binaryCombos(n){
    var result = [];
    for(y=0; y<Math.pow(2,n); y++){         // This cycles through the maximum number of combinations 
                                               (power of 2 because it's binary)
        
    var combo = [];                         // combo for particular iteration, eventually pushed to result
        for(x=0; x<n; x++){                 // iterate over number of booleans in array
            //shift bit and and it with 1
            if((y >> x) & 1)                // right shifts and then masks for 1? i.e. tests if it's odd??
                combo.push(true);           // I don't see how this ensures all combiantions are covered
             else 
                combo.push(false);          // I don't understand the criteria for pushing false or true :(        
        }
        result.push(combo);
    }
    return result;
}

//Usage
combos = binaryCombos(3);


for(x=0; x<combos.length; x++){               // This looks like driver code so have been ignoring this
    console.log(combos[x].join(','));
}

这是一个使用 n = 2 的示例:

y = 0 x = 0

0 >> 0 仍然为 0,因此当与 1 进行“与”运算时将评估为 false:

0000 0000 & 0000 0001 --> 0

'false' 推送到组合数组

y=0 x=1

0 >> 1 仍然为 0 并将 'false' 推送到组合数组

推送到结果:[false, false]

y=1 x=0

1 >> 0 等于 0000 0001 没有移位(?)所以 'anding' 与 1 将评估为真, 'true' 推送到组合数组

y=1 x=1

1 >> 1 与减半相同,但会 eval 为 0,因此 false 被推送到组合数组

推送到结果:[true, false]

y=2 x=0

2 >> 0 等于 false 被推送到组合数组为 0000 0010 & 0000 0001 = 0

y=2 x=1

2 >> 1 等于 true 被推送为 0000 0001 & 0000 0001 = 1

推送到结果:[false, true]

y=3 x=0

3 >> 0 等于 true 被推送到组合数组,因为 0000 0011 & 0000 0001 = 1

y=3 x=1

3 >> 1 等于 true 被推送到组合数组

推送到结果:[true, true]

返回结果:[[false, false], [true, false], [false, true], [true, true]]

我可以直观地看到嵌套循环将有助于解决排列问题,并且我可以认识到这已经得出了正确的答案,但看不到将 y 移动 x 和“与”之间的关系,并全面覆盖所有组合。

任何帮助表示赞赏!

【问题讨论】:

  • 我知道您想在阅读完答案后对您所理解的内容做一些笔记,但这确实不属于问题部分。如果您想为未来的读者留下关于您的理解的注释,请将其发布为答案,而不是对您的问题的编辑。

标签: algorithm bitwise-operators bit-shift bitwise-and


【解决方案1】:

x 是一个(从零开始的)位数。该位编号指的是y 的二进制表示中的一位:最低有效位的编号为0(在二进制表示的右侧),最高有效位的编号为n-1。由于组合具有n 布尔值,因此(y 的)位表示具有n 位。零位转换为false,一位转换为true

现在,要从y 的二进制表示中提取xth 位,我们可以使用移位运算符:

y >> x

经过这个操作,我们感兴趣的位已经一直向右移动了。 xth 位右侧的所有位都已通过此 &gt;&gt; 操作“下降”。剩下的就是去掉我们要提取的位 left 的所有位。为此,我们使用&amp; 1。这就是隔离y 的二进制表示的xth 位所需的全部内容。

示例

假设我们有n=4,外部循环已经迭代到y=13y 的二进制表示为 1101。

x 上的循环将从x=0 开始,因此移位操作不会执行任何操作,但&amp; 1 操作将提取 1101 的最右边位,即 1。

下一次内部迭代将有x=1。现在移位操作将踢出最右边的位,剩下 110。&amp; 1 操作现在将提取 0。所以它继续为 x=3x=4

这里用表格表示(n=4y=13):

y x y &gt;&gt; x (y &gt;&gt; x) &amp; 1 boolean
1101 0 1101 1 true
1101 1 110 0 false
1101 2 11 1 true
1101 3 1 1 true

【讨论】:

  • 非常感谢 Trincot - 我已经进行了编辑以帮助我处理您的回答向我展示的内容。
猜你喜欢
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 2018-05-18
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 2012-10-11
  • 2018-05-20
相关资源
最近更新 更多