Binary Logic Operations on BigInteger

suggest change

BigInteger supports the binary logic operations that are available to Number types as well. As with all operations they are implemented by calling a method.

Binary Or:

BigInteger val1 = new BigInteger("10");
BigInteger val2 = new BigInteger("9");

val1.or(val2);
Output: 11 (which is equivalent to 10 | 9)

Binary And:

BigInteger val1 = new BigInteger("10");
BigInteger val2 = new BigInteger("9");

val1.and(val2);
Output: 8 (which is equivalent to 10 & 9)

Binary Xor:

BigInteger val1 = new BigInteger("10");
BigInteger val2 = new BigInteger("9");

val1.xor(val2);
Output: 3 (which is equivalent to 10 ^ 9)

RightShift:

BigInteger val1 = new BigInteger("10");

val1.shiftRight(1);   // the argument be an Integer
Output: 5 (equivalent to 10 >> 1)

LeftShift:

BigInteger val1 = new BigInteger("10");

val1.shiftLeft(1);   // here parameter should be Integer
Output: 20 (equivalent to 10 << 1)

Binary Inversion (Not)‚ÄČ:

BigInteger val1 = new BigInteger("10");

val1.not();
Output: 5

NAND (And-Not):*

BigInteger val1 = new BigInteger("10");
BigInteger val2 = new BigInteger("9");

val1.andNot(val2);
Output: 7

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents