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

2

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

6

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.

8

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.

0

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

1

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 = 0x7ffffff) which is one less what -Integer.MIN_VALUE should be.

1

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.

original *= -1;

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

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 mediacre programmers don’t understand what’s going on 😉

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

1

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.

For converting a negative number to positive. Simply use Math.abs() inbuilt function.

int n = -10;
n = Math.abs(n);

All the best!

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 🙂

3

You can use Math:

int x = Math.abs(-5);

3

Not the answer you’re looking for? Browse other questions tagged java math or ask your own question.