Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么该算法在Java中使用按位and运算符?_Java_Algorithm - Fatal编程技术网

为什么该算法在Java中使用按位and运算符?

为什么该算法在Java中使用按位and运算符?,java,algorithm,Java,Algorithm,我看到一种算法,例如: for (int i = 1; i < sums.length; i++) { /* * dynamic programming: * 1. remove a single block from the current subset of blocks * 2. the corresponding block sum was already calculated * 3.

我看到一种算法,例如:

    for (int i = 1; i < sums.length; i++) {
        /*
         * dynamic programming:
         * 1. remove a single block from the current subset of blocks
         * 2. the corresponding block sum was already calculated
         * 3. add the number on the removed block to it
         *
         * here: always choose the block corresponding
         * to the least significant bit of i
         */
        int t = Integer.numberOfTrailingZeros(i & -i);
        sums[i] = sums[i & ~(1 << t)] + block[t];

        //only add block subsets that add up to a face
        if (masks.containsKey(sums[i]))
            masks.get(sums[i]).add(i);
    }
for(int i=1;isums[i]=sums[i&~(1是的,你可以这样做。一些合理的解释:

  • 最初的版本移植到一个有自制版本的东西上,他把这个技巧留在了那里
  • 我不理解这个整数。numberOfTrailingZeros()不在乎是否还有其他有效位,并且知道如何快速获取最有效位的值
  • 或者最初有一个家用brew版本,它刚刚使用i&-i和位移位,直到值为零并将其设置为等于t。有人只是将该操作替换为内置操作,而没有意识到i&-i是一个使该操作正常工作的技巧删除的操作正常工作。与其他检查相比,检查零的速度更快,尽管它没有这已经不重要了,所以一些超优化的人经常会安排一些东西来检查零,特别是如果他们不需要减法的话
for(变量i=0;i<1000;i++){
文件。编写(i和-i);
文件。写(“
”);
}
我只是想展示一下我&-I的样子。这在其他方面没有任何帮助。我认为网页中没有内置“运行代码片段”按钮的java代码。但是将var改为int,将document.write()改为System.println()并删除关于
的部分。脚本显示在适当的位置。它不依赖于外部链接。它正确地显示了i&-i随着i的增加所做的事情。如果我认为它有争议,我就不会麻烦了。关键是它保留了最不重要的1,而没有其他内容,如果你在检查0,或者认为算法会uld需要它,但考虑到它的用途,其他的就不需要了。但是,一个单位,基本上是免费的。