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?

Community
  • 1
  • 1
  • 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. – robotlos May 05 '16 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. –  May 05 '16 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. –  May 05 '16 at 19:24

2 Answers2

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

robotlos
  • 536
  • 2
  • 10
  • 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. –  May 06 '16 at 13:31
  • 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? – Hanzhou Tang May 01 '19 at 01:14
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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • ok, thank you. what lengths could be transformed using the "shortcut"? –  May 05 '16 at 19:29
  • 1
    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. – robotlos May 05 '16 at 19:31
  • @DanyLavrov, what a good idea for a supplementary exercise! I admire your incentive! – John Bollinger May 05 '16 at 19:39
  • @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. –  May 05 '16 at 19:50