[Java] Consideration when handling negative binary numbers with Integer.parseInt ()

Operating environment

Java11

Contents

It's not a big deal, so I'll suddenly get into the main subject.

In Java, negative numbers are internally represented by two's complement. You can use Integer.toBinaryString () to get a string with an internal representation.

Two's complement is the number obtained by inverting all bits and then adding 1 in the case of binary numbers. Decimal number 15 is 1111 in decimal number, so -15 in decimal number is the following value.

System.out.println(Integer.toBinaryString(-15))  // 11111111111111111111111111110001

However, passing this value to Integer.parseInt () will result in an error.

Integer.parseInt(Integer.toBinaryString(-15), 2)
//Exception java.lang.NumberFormatException: For input string: "11111111111111111111111111110001"
//        at NumberFormatException.forInputString (NumberFormatException.java:65)
//        at Integer.parseInt (Integer.java:652)
//        at (#9:1)

Apparently, you need to pass it to Integer.parseInt () in the form of a positive binary number with a negative sign, rather than the value of the internal 2's complement representation.

System.out.println(Integer.parseInt("-1111", 2))  // -15

that's all.

2019/3/4 postscript You pointed out some in the comments.

https://docs.oracle.com/javase/jp/8/docs/api/java/lang/Integer.html#toBinaryString-int-

Returns the string representation of the integer argument as an unsigned integer of radix 2. An unsigned int value is the argument plus 2 ^ 32 if the argument is a negative number.

(The part that becomes 232 when copied as it is is corrected to 2 ^ 32)

It was stated that if the argument is a negative value, the internal 2's complement representation is returned, but the official document seems to have the above description. However, the result is the same. I verified it using Ruby. (Because Java overflows and is troublesome)

First, the return value of Java's Integer.toBinaryString ()

System.out.println(Integer.toBinaryString(-15));  // 11111111111111111111111111110001

Binary representation of the value of the argument (-15) plus 2 ^ 32

n = -15 + 2 ** 32
puts(n.to_s(2))   # 11111111111111111111111111110001

It was confirmed that both became 11111111111111111111111111110001.

Also, there seems to be a method called Integer.parseUnsignedInt () that can parse this internal binary number as it is.

System.out.println(Integer.parseUnsignedInt(Integer.toBinaryString(-15), 2));  // -15

Thank you for pointing out. (It's a story of reading the official document in the first place ...)

Recommended Posts

[Java] Consideration when handling negative binary numbers with Integer.parseInt ()
Error when playing with java
Memo when HTTP communication with Java (OkHttp)
When calling API with java, javax.net.ssl.SSLHandshakeException occurs
[Java] Precautions when comparing character strings with character strings
Specify ClassPath when using jupyter + Java with WSL
Refactor property access handling with Java Method Util
[Java] Find prime numbers with an Eratosthenes sieve
Distinguish between positive and negative numbers in Java
Let's create a TODO application in Java 11 Exception handling when accessing TODO with a non-existent ID