BigInteger negate() Method in Java
Last Updated :
06 Apr, 2023
Prerequisite: BigInteger Basics The java.math.BigInteger.negate() method returns a BigInteger whose value is (- this). negate() method will change the signed bit of BigInteger. Syntax:
public BigInteger negate()
Parameters: The method does not accept any parameter. Return Value: The method returns the operation of (- this). Examples:
Input: value = 2300
Output: -2300
Explanation:
Binary signed 2's complement of 2300 = 0000100011111100
Signed bit are 0000
change sign bit to 1111
so negate of 0000100011111100 in signed 2's complement is 1111011100000100
Decimal value = -2300.
Input: value = 567689
Output: -567689
Below program illustrates negate() method of BigInteger.
Java
import java.math.*;
public class GFG {
public static void main(String[] args)
{
BigInteger biginteger = new BigInteger(" 2300 ");
BigInteger finalvalue = biginteger.negate();
String result = "Result of negate operation on " +
biginteger + " is " + finalvalue;
System.out.println(result);
}
}
|
Output:
Result of negate operation on 2300 is -2300
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#negate()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...