larrydavid2003
BAN USERpublic static long sum(String x, String y)
{
if(x == null || y == null)
throw new IllegalArgumentException("X or/and y is null");
if(x.length() > 64 || y.length() > 64)
throw new IllegalArgumentException("xlen, ylen exceeds 64");
long xl = parseAsLong(x);
System.out.println(xl);
long yl = parseAsLong(y);
System.out.println(yl);
return xl + yl;
}
private static long parseAsLong(String s)
{
long l = 0L;
int n = s.length();
for(int i = 0; i < n; i++) {
char c = s.charAt(i);
if(c == '0')
continue;
else if(c == '1')
l |= (1L << (n - i - 1));
else
throw new IllegalArgumentException("Invalid character at index" + i);
}
return l;
}
- larrydavid2003 May 20, 2015