binary summation with exact precision

I am using following code to do binary summation:

class dump{
public static void main(String[] args){
    final int number0 = Integer.parseInt("000", 2);
    final int number1 = Integer.parseInt("1", 2);

    final int sum = number0 + number1;
    System.out.println(Integer.toBinaryString(sum));

}
}

The problem is that I do not get the exact precision of the answer - eg 000 + 1 or 00 + 1 both resolve to answer as 1. Since I intend to use the result as the key in a hashmap ,the non-uniqueness of the result is a problem. How do I get 00+1=>01 or 000+1 =>001.

Answers


You will need to save the maximum length among all binary numbers. Could be dynamically if you want. The following may give you an idea.

final int number0 = Integer.parseInt("000", 2);
final int number1 = Integer.parseInt("1", 2);
final int sum = number0 + number1;

int len = 3;

String output = String.format("%" + len + "s", Integer.toBinaryString(sum))
        .replace(' ', '0');
System.out.println(output); // 001

The problem is that I do not get the exact precision of the answer

Insignificant leading zeros have no effect on the precision of the answer: the answer 1 is exactly as precise as 001 or 00000000000001 - it is the same number.

Since I intend to use the result as the key in a hashmap ,the non-uniqueness of the result is a problem.

It would be a problem if you were to use such a string as a key in a sorted map, because the keys would sometimes appear out of order. In a hash map, it is not a problem, because Integer.toBinaryString(sum) will drop leading zeros in all numbers, so you should never see numbers like 000 produced by your procedure. If such numbers do come from the outside, you can normalize them by parsing and converting to binary before using them as a hash key.


The way I understand your question is that you want "000" + "1" to become "001", and "00010" + "10" to become "00100". Even though I could question whether what you are trying to do here is right, correct or a proper way to do it. I will only try to solve the problem for you:

You will need to check the length of your inputs and then append enough zeroes.

public static String binSum(String a, String b) {
    final int number0 = Integer.parseInt(a, 2);
    final int number1 = Integer.parseInt(b, 2);
    final int sum = number0 + number1;
    String result = Integer.toBinaryString(sum);
    while (result.length() < Math.max(a.length(), b.length())) result = "0" + result;
    return result;
}

System.out.println(binSum("000", "1"));  // 001
System.out.println(binSum("0000", "1")); // 0001
System.out.println(binSum("10", "10"));  // 100

java.lang.Integer.toBinaryString() method returns the string representation of the unsigned integer value represented by the argument in binary (base 2).

System.out.println("Binary is " + Integer.toBinaryString(170));

Output is:

Binary is 10101010

Suggest to use some other technique to generate unique string from integer input.

For example, you can use encoded string as follows:

byte[] encodedBytes = Base64.encodeBase64(new String(sum).getBytes());
System.out.println("Key " + new String(encodedBytes));

Need Your Help

Null pointer Exception in Java when assigning value to int array

java

My code shows null pointer exception when i assign values to int array in the object:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.