本文整理汇总了Java中java.math.BigInteger.andNot方法的典型用法代码示例。如果您正苦于以下问题:Java BigInteger.andNot方法的具体用法?Java BigInteger.andNot怎么用?Java BigInteger.andNot使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.math.BigInteger
的用法示例。
在下文中一共展示了BigInteger.andNot方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.math.BigInteger; //导入方法依赖的package包/类
public static void main(String[] args) {
Scanner scann = new Scanner(System.in);
String inputNumber = scann.nextLine();
/*BigInteger mask = BigInteger.valueOf(7L);
mask = mask.shiftLeft(61);*/
BigInteger mask = new BigInteger("e000000000000000", 16);
BigInteger num = new BigInteger(inputNumber);
// System.out.printf("%16s -> %s%n", "mask length", mask.toString(2).length());
// System.out.printf("%16s -> %s%n", "binary mask",mask.toString(2));
// System.out.printf("%16s -> %s%n", "binary num",num.toString(2));
while (mask.compareTo(BigInteger.valueOf(7L)) >= 0){
//System.out.printf("%16s -> %s%n", "mask & num", mask.and(num).toString(2));
boolean treeConsecutiveOnes = Objects.equals(num.and(mask), mask);
boolean treeConsecutiveZeroes = Objects.equals(num.and(mask), BigInteger.ZERO);
if(treeConsecutiveOnes || treeConsecutiveZeroes){
if(treeConsecutiveOnes){
//num &= (~mask)
num = num.andNot(mask);
//System.out.printf("%16s -> %s%n", "num &= (~mask)", num.toString(2));
} else {
// num |= mask
num = num.or(mask);
//System.out.printf("%16s -> %s%n", "num |= mask", num.toString(2));
}
// move mask right
mask = mask.shiftRight(3);
//System.out.printf("%16s -> %s%n", "mask >> 3 ", mask.toString(2));
} else {
mask = mask.shiftRight(1);
//System.out.printf("%16s -> %s%n","mask >> 1", mask.toString(2));
}
}
System.out.println(num);
}