Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaBigInteger and() Method in Java

BigInteger and() Method in Java

The java.math.BigInteger.and(BigInteger val) method returns a BigInteger whose value is bitwise-AND of two BigIntegers. This method returns a negative number if both of the BigIntegers are negative. The and() method applies bitwise-AND operation upon the current bigInteger and bigInteger passed as parameter.
Syntax: 

public BigInteger and(BigInteger val)

Parameters: The method accepts one parameter val of BigInteger type and refers to the value to be AND’ed with the current BigInteger.
Return Value: The method returns the value of bitwise-AND of two BigIntegers.
Examples: 

Input: value1 = 2300, value2 = 3400
Output: 2120
Explanation:
Binary of 2300 = 100011111100
Binary of 3400 = 110101001000
AND of 100011111100 and 110101001000 = 100001001000
Decimal of 100001001000 = 2120.

Input: value1 = 432045, value2 = 321076
Output: 296484

Below program is used to illustrate the and() method of BigInteger. 

Java




/*
*Program Demonstrate and() method of BigInteger
*/
import java.math.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Creates 2 BigInteger objects
        BigInteger biginteger = new BigInteger("2300");
        BigInteger val = new BigInteger("3400");
 
        // Call and() method to find this & val
        BigInteger biggerInteger = biginteger.and(val);
 
        String result = "Result of AND operation between " + biginteger + " and "
                        + val + " is " + biggerInteger;
 
        // Print the result
        System.out.println(result);
    }
}


Output: 

Result of AND operation between 2300 and 3400 is 2120

 

Reference:https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#and(java.math.BigInteger)

RELATED ARTICLES

Most Popular

Recent Comments