BigInteger的“&”逻辑运算符等效

时间:2015-10-30 20:21:13

标签: java if-statement biginteger

我有这段代码:

if ((x & y)==0){ 
// do this... 
}

我想达到相同的效果,但使用BigInteger而不是int。

我试过了:

if ((x.compareTo(BigInteger.ZERO)==0)&&(y.compareTo(BigInteger.ZERO)==0)){ 
// do this...
}

然而,现在,我的程序永远不会进入这个if语句。我非常感谢你的帮助。

此外,这是整个代码。

import java.math.*;

public class mew {

public static void main (String[] args) {

BigInteger two = BigInteger.valueOf(2);
BigInteger num = two.pow(100);
BigInteger i = BigInteger.valueOf(0);

while (i.compareTo(num) < 0){
    BigInteger mask = num;

    while (mask.compareTo(BigInteger.ZERO) > 0){
            if ((mask.compareTo(BigInteger.ZERO)==0)&&(i.compareTo(BigInteger.ZERO)==0)){
             System.out.print("0");
             }
            else {
             System.out.print("1");
                }
             mask = mask.shiftRight(1);
                }
     System.out.println();
     i = i.add(BigInteger.valueOf(1));
          }

    }
}

目的是打印n长位串的所有可能排列。我应该参考我的想法和实现的位置:Java: How to output all possible binary combinations (256 different sequences)?参见nikis post。

1 个答案:

答案 0 :(得分:5)

相当于

if ((x & y)==0){ 
    // do this... 
}
BigInteger的

if (x.and(y).equals(BigInteger.ZERO)) {
    // do this...
}

如果x和y都为零&#34;

这两件事情都不一样。