JDK-7079610 : Parsing of binary representation of Long fails
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 6u26
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_vista
  • CPU: x86
  • Submitted: 2011-08-16
  • Updated: 2012-03-20
  • Resolved: 2011-08-17
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.0.6002]

A DESCRIPTION OF THE PROBLEM :
According to the documentation using negative numbers while parsing Longs should be allowed, as the java docs states:

     * parseLong("-0", 10) returns 0L
     * parseLong("-FF", 16) returns -255L
     * parseLong("1100110", 2) returns 102L


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
		System.out.println(Long.parseLong(Long.toBinaryString(Long.MIN_VALUE),2));



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
-9223372036854775808

ACTUAL -
Exception in thread "main" java.lang.NumberFormatException: For input string: "1111111111111111111111111111111111111111111111111111111111111111"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
	at java.lang.Long.parseLong(Long.java:422)
	at Test.main(Test.java:4)


ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.NumberFormatException: For input string: "1111111111111111111111111111111111111111111111111111111111111111"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
	at java.lang.Long.parseLong(Long.java:422)
	at Test.main(Test.java:4)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
System.out.println(Long.parseLong(Long.toBinaryString(Long.MIN_VALUE),2));
---------- END SOURCE ----------

Comments
EVALUATION The binary strings produces for negative values by Long.toBinaryString are unsigned while the strings accepted by parseLong for base 2 are signed. Closing as a duplicate of 4215269.
17-08-2011