1

I am trying to find a quick/easy way to convert a twos complement binary string into a negative decimal number. I have tried to use the method presented in this question but it does not work. This is the code i am trying to run:

short res = (short)Integer.parseInt("1001", 2);
System.out.println(res);

When i run this code the result is 9. Am i missing something? What am i doing wrong?

3
  • You're not applying any logic as to what a two's complement number is with this code. You know why it's a negative number. You're going to have to break up the number you're trying to parse and add some more logic to it. Not just convert it from binary to decimal. Commented May 5, 2016 at 19:20
  • what logic should i apply? if my binary string start with a '1' i what to decode it as a negative number. Commented May 5, 2016 at 19:22
  • if my original number is 7--> 111, if i want to represent -7 its binary form will be 1001. all i want is to decode the '1001' as -7. Commented May 5, 2016 at 19:24

2 Answers 2

2

Following the Two's Complement algorithm, I wrote the following:

public static int getTwosComplement(String binaryInt) {
    //Check if the number is negative.
    //We know it's negative if it starts with a 1
    if (binaryInt.charAt(0) == '1') {
        //Call our invert digits method
        String invertedInt = invertDigits(binaryInt);
        //Change this to decimal format.
        int decimalValue = Integer.parseInt(invertedInt, 2);
        //Add 1 to the curernt decimal and multiply it by -1
        //because we know it's a negative number
        decimalValue = (decimalValue + 1) * -1;
        //return the final result
        return decimalValue;
    } else {
        //Else we know it's a positive number, so just convert
        //the number to decimal base.
        return Integer.parseInt(binaryInt, 2);
    }
}

public static String invertDigits(String binaryInt) {
    String result = binaryInt;
    result = result.replace("0", " "); //temp replace 0s
    result = result.replace("1", "0"); //replace 1s with 0s
    result = result.replace(" ", "1"); //put the 1s back in
    return result;
}

Here are some sample runs:

run:
Two's Complement of: 1000: -8
Two's Complement of: 1001: -7
Two's Complement of: 1010: -6
Two's Complement of: 0000: 0
Two's Complement of: 0001: 1
Two's Complement of: 0111: 7

Sign up to request clarification or add additional context in comments.

2 Comments

had the same idea, just slightly different. change to decimal first, then minus 1, then to binary and flip bits, then back to decimal again times -1.
It seems like when binaryInt ="10000000000000000000000000000000", the decimalValue + 1 will overflow. But when I test the function, it returns the correct result. Could you please tell me why it happens?
0

When i run this code the result is 9.

As it should be.

Am i missing something? What am i doing wrong?

The difference between your code and the answer you referenced is the number of bits in the input. "Two's complement" is not well defined without specifying a width. The answer you copied is for 16-bit two's complement, because Java shorts are 16 bits wide. If you want 4-bit two's complement then there is no corresponding Java data type, so you'll not be able to take the same shortcut.

4 Comments

ok, thank you. what lengths could be transformed using the "shortcut"?
This looks like a HW assignment. Why take shortcuts? Not that they aren't great, but just because I'm sure your professor would want to see you use logic to parse the number.
@DanyLavrov, what a good idea for a supplementary exercise! I admire your incentive!
@robotlos close enough, i am actually 33 and i just needed it for my work. just did not wanted to write something that i thought was already existed. figured it out, thank you all for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.