I am trying to do a XOR operation on two 64 bits Long variables in Java. The problem is that it fails when I add more than 16 bits in a variable.
For instance this works and returns 7:
Long h1 = Long.parseLong("1100001101001101");
Long h2 = Long.parseLong("1100001101000001");
System.out.println(Long.bitCount(h1 ^ h2));
If I increase the value of h1 and h2 to:
Long h1 = Long.parseLong("11000110000110100110101101001101");
Long h2 = Long.parseLong("11000011100001101001101101000001");
I get an error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "11000110000110100110101101001101"
at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:592)
at java.lang.Long.parseLong(Long.java:631)
Same if I double it (64 bits what I would want to compute):
Long h1 = Long.parseLong("1100011100011000011010011010110100110110000110100110101101001101");
Long h2 = Long.parseLong("1100001110001100001101001101011010011011100001101001101101000001");
Any help on why this fails above 16 bits?
Long.parseLong("11000110000110100110101101001101")
attempts to parse theString
as decimal number, and this number is too large to fit in along
variable.To parse the
String
as a binary number you need to specify the radix:BTW,
Long.parseLong
returns along
, so I'd either assign it to along
:or use
Long.valueOf
: