vendredi 30 octobre 2015

BigInteger's "&" Logical Operator Equivalent

I have this code:

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

I would like to achieve the same effect but using BigInteger instead of int.

I tried this:

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

Now, however, my program is never going inside this if statement. I would really appreciate your help.

Also, here's the entire code.

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));
          }

    }
}

The purpose is to print all possible permutations of an n-long bit string. I ought to reference where I got the idea and the implementation: Java: How to output all possible binary combinations (256 different sequences)? see nikis post.

Aucun commentaire:

Enregistrer un commentaire