Java.math.BigInteger.andNot() Method



The java.math.BigInteger.andNot(BigInteger val) returns a BigInteger whose value is (this & ~val). This method, which is equivalent to and(val.not()), is provided as a convenience for masking operations. This method returns a negative BigInteger if and only if this is negative and val is positive.


Following is the declaration for java.math.BigInteger.andNot() method

public BigInteger andNot(BigInteger val)


  • val - value to be complemented and AND'ed with this BigInteger

Return Value

This method returns a BigIntger object of value, this & ~val


  • NA


The following example shows the usage of math.BigInteger.andNot() method

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

	// create 3 BigInteger objects
	BigInteger bi1, bi2, bi3;  

	// assign values to bi1, bi2
	bi1 = new BigInteger("6"); //110
	bi2 = new BigInteger("3"); //011

	// perform andNot operation on bi1 using bi2
	bi3 = bi1.andNot(bi2);
	String str = "Result of andNot operation is " +bi3;;

	// print bi3 value
	System.out.println( str );

Let us compile and run the above program, this will produce the following result:

Result of andNot operation is 4