BigInteger negate() Method in Java

Prerequisite: BigInteger Basics
The java.math.BigInteger.negate() method returns a BigInteger whose value is (- this). negate() method will change the singned 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
Singed bit are 0000
change sing 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

/*
*Program Demonstrate negate() method of BigInteger 
*/
import java.math.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create  BigInteger object
        BigInteger biginteger = new BigInteger("2300");
  
        // Call negate() method to find -this
        BigInteger finalvalue = biginteger.negate();
        String result = "Result of negate operation on "
        biginteger + " is " + finalvalue;
  
        // Prints result
        System.out.println(result);
    }
}

chevron_right


Output:

Result of negate operation on 2300 is -2300

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.