Reputation: 1011
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
Upvotes: 82
Views: 277042
Reputation: 1041
Adding my 2 cents.
public static int reverse(int value) {
return Integer.valueOf(0).compareTo(value * value) * value;
}
Tip : you can replace 0
with java.math.BigDecimal.ZERO
or java.math.BigInteger.ZERO
.
To test it (i.e. on tio.run) :
public class Main {
public static void main(String[] args) {
print(5);
print(-5);
}
public static int reverse(int value) {
return Integer.valueOf(0).compareTo(value * value) * value;
}
public static void print(int i) {
System.out.printf("\nReversed %s to %s .", i, reverse(i));
}
}
Prints :
Reversed 5 to -5 .
Reversed -5 to 5 .
Processing time : 0.106 s.
Instead of doing the x
to -x
thing.
public class Main {
public static void main(String[] args) {
int x = 5;
print(x);
x = -5;
print(x);
}
public static void print(int i) {
System.out.printf("\nReversed %s to %s .", i, -i);
}
}
Processing time : 0.108 s
Upvotes: 0
Reputation: 622
For converting a negative number to positive. Simply use Math.abs() inbuilt function.
int n = -10;
n = Math.abs(n);
All the best!
Upvotes: 0
Reputation: 726
In kotlin you can use unaryPlus and unaryMinus
input = input.unaryPlus()
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/unary-plus.html https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/unary-minus.html
Upvotes: -2
Reputation: 93
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
Upvotes: 5
Reputation: 82136
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 ;)
Upvotes: 6
Reputation: 79
original *= -1;
Simple line of code, original is any int you want it to be.
Upvotes: 5
Reputation: 21
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.
Upvotes: 1
Reputation: 101
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.
Upvotes: 8
Reputation: 7824
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.
Upvotes: 6
Reputation: 424953
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:
0 - x
), andThis compiles and works as expected.
Upvotes: 57
Reputation: 1452
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
Upvotes: 11
Reputation: 310840
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.
Upvotes: 116
Reputation: 26920
What about x *= -1;
? Do you really want a library function for this?
Upvotes: 288