-3

My question: how to make -2147483648 a positive number in Java?

My thoughts: There are many ways to make a negative number positive:

Make a negative number positive

however, for the edge case, where the min value of a int in Java is -2147483648, all those methods do not work.

What should I do? Thank you very much!

Helen Cui
  • 181
  • 2
  • 11
  • 2
    What value do you expect? Please clarify what you mean. – Elliott Frisch Nov 15 '18 at 01:56
  • `2147483648` is out of the range of an int. What should the positive value be for `-2147483648`? – GBlodgett Nov 15 '18 at 01:57
  • 1
    That's beyond the limitations for int. You need to use a different datatype. – clinomaniac Nov 15 '18 at 01:57
  • if (num == -2147483648) num = 27; – Scary Wombat Nov 15 '18 at 02:00
  • @GBlodgett I was doing a leetcode practice. https://leetcode.com/problems/reverse-integer/ . The question asks to reverse a number. The method is limited to receive a int datatype where the input is -2147483648. I want to get rid of the sign before I put it into StringBuilder and reverse it. And now realising I can not make it positive simply. Thanks. – Helen Cui Nov 15 '18 at 02:01
  • @HelenCui From the problem description: `For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows` – GBlodgett Nov 15 '18 at 02:08

2 Answers2

5

If you care about it staying an int: you can't. The int primitive fits only values from -2147483648 up to and including 2147483647.

However, if you just want to turn -2147483648 into the integer number 2147483648, then use a long:

public class Test
{
  public static void main(String[] args)
  {
    int v = -2147483648;
    long r = -(long)v;
    System.out.print(r);
  }
}

And done: 2147483648.

Of course, also read through https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html to get familiar with the limitations of all the primitive data types. And if that still isn't enough and you need arbitrary length integers, BigInteger is available.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • if that's still not enough (you'll have the same problem again at Long.MIN_VALUE), you can use BigInteger – kpcraig Nov 15 '18 at 02:01
  • The input int cannot be Long.MIN_VALUE – harold Nov 15 '18 at 02:05
  • 1
    Keith means that if, after switching to `long` data types, you end up in a situation where you need some `n` that is either less than `Long.MIN_VALUE` or higher than `Long.MAX_VALUE`, you'll want to use a BigInteger, which has no value limitation, at the cost of also not having a fixed memory footprint or standard operator based arithmetic. – Mike 'Pomax' Kamermans Nov 15 '18 at 02:06
2

My question: how to make -2147483648 a positive number in Java?

There is no value that is representable as an int that is equal to +2147483648.

So the idea of "making -2147483648 positive" as an int has no meaning. You simply cannot do it1.

But you could represent +2147483648 as a long or as a BigInteger (or a few other ways with various caveats.)


What should I do?

Use long (though that has the same issue with Long.MIN_VALUE) or BigInteger or BigDecimal or some other representation.

Or just treat this as a "feature" rather than a problem ... and work around it. For example, remove the sign after you have put it into the StringBuilder or treat -2147483648 as a special case.


1 - It is a mathematical fact that 2's complement signed representations are not symmetric about zero. There is one more negative number ( < 0) than positive ( > 0), and therefore one negative number doesn't have a corresponding positive number.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216