Java math function to convert positive int to negative and negative to positive?

JavaMath

Java Problem Overview


Is there a Java function to convert a positive int to a negative one and a negative int to a positive one?

I'm looking for a reverse function to perform this conversion:

-5  ->  5
 5  -> -5

Java Solutions


Solution 1 - Java

What about x *= -1; ? Do you really want a library function for this?

Solution 2 - Java

x = -x;

This is probably the most trivial question I have ever seen anywhere.

... and why you would call this trivial function 'reverse()' is another mystery.

Solution 3 - Java

Just use the unary minus operator:

int x = 5;
...
x = -x; // Here's the mystery library function - the single character "-"

Java has two minus operators:

  • the familiar arithmetic version (eg 0 - x), and
  • the unary minus operation (used here), which negates the (single) operand

This compiles and works as expected.

Solution 4 - Java

Another method (2's complement):

public int reverse(int x){
    x~=x;
    x++;
    return x;
}

It does a one's complement first (by complementing all the bits) and then adds 1 to x. This method does the job as well.

Note: This method is written in Java, and will be similar to a lot of other languages

Solution 5 - Java

No such function exists or is possible to write.

The problem is the edge case Integer.MIN_VALUE (-2,147,483,648 = 0x80000000) apply each of the three methods above and you get the same value out. This is due to the representation of integers and the maximum possible integer Integer.MAX_VALUE (-2,147,483,647 = 0x7fffffff) which is one less what -Integer.MIN_VALUE should be.

Solution 6 - Java

Yes, as was already noted by Jeffrey Bosboom (Sorry Jeffrey, I hadn't noticed your comment when I answered), there is such a function: Math.negateExact.

and

No, you probably shouldn't be using it. Not unless you need a method reference.

Solution 7 - Java

original *= -1;

Simple line of code, original is any int you want it to be.

Solution 8 - Java

Necromancing here.
Obviously, x *= -1; is far too simple.

Instead, we could use a trivial binary complement:

number = ~(number - 1) ;

Like this:

import java.io.*;
 
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int iPositive = 15;
        int iNegative = ( ~(iPositive - 1) ) ; // Use extra brackets when using as C preprocessor directive ! ! !...
        System.out.println(iNegative);

        iPositive =  ~(iNegative - 1)  ;
        System.out.println(iPositive);

        iNegative = 0;
        iPositive = ~(iNegative - 1);
        System.out.println(iPositive);


    }
}

That way we can ensure that mediocre programmers don't understand what's going on ;)

Solution 9 - Java

The easiest thing to do is 0- the value

for instance if int i = 5;

0-i would give you -5

and if i was -6;

0- i would give you 6

Solution 10 - Java

You can use the minus operator or Math.abs. These work for all negative integers EXCEPT for Integer.MIN_VALUE! If you do 0 - MIN_VALUE the answer is still MIN_VALUE.

Solution 11 - Java

We can reverse Java number int or double using this :

int x = 5;
int y = -7;

x = x - (x*2); // reverse to negative
y = y - (y*2); // reverse to positif

Simple algorithm to reverse number :)

Solution 12 - Java

Solution 13 - Java

You can use Math:

int x = Math.abs(-5);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzippyView Question on Stackoverflow
Solution 1 - JavaFailedDevView Answer on Stackoverflow
Solution 2 - Javauser207421View Answer on Stackoverflow
Solution 3 - JavaBohemianView Answer on Stackoverflow
Solution 4 - JavaShivanshu GoyalView Answer on Stackoverflow
Solution 5 - JavaSalix albaView Answer on Stackoverflow
Solution 6 - Javaold grouchView Answer on Stackoverflow
Solution 7 - JavaK.D. CodeView Answer on Stackoverflow
Solution 8 - JavaStefan SteigerView Answer on Stackoverflow
Solution 9 - JavaJebjoshView Answer on Stackoverflow
Solution 10 - JavalovosView Answer on Stackoverflow
Solution 11 - JavaJohanView Answer on Stackoverflow
Solution 12 - JavaEmmanuel LoisanceView Answer on Stackoverflow
Solution 13 - JavaThiago AraújoView Answer on Stackoverflow