0

I have binary String To Integer method implemented in my code. Problem is that I'm getting wrong last two digits of method output results, e.g.:

SHOULD BE RESULT : 11111111100001001000101110000100

RESULT : 11111111100001001000101110000011

There is always problem with last two digits of result.

Any help would be greatly appreciated.

code:

public static int binaryStringToInteger (String binaryString){
char[] digits = binaryString.toCharArray();
int binaryInteger = 0;
int count = 0;
for(int i=digits.length-1;i>=0;i--)
  {
    if(digits[i]=='1') 
         {
          binaryInteger +=(int)Math.pow(2, count);
         }
    count++;
  }
 return binaryInteger;
}
10
  • 1
    I don't understand one thing, namely you wrote that expected result is 11111111100001001000101110000100, while your method is returning an int. Commented May 22, 2015 at 23:27
  • @Suspended method is returning int, and result I'm getting and expected one are printed with Integer.toBinaryString(binaryInteger); Commented May 22, 2015 at 23:31
  • Thank you, that clears up the fog. Commented May 22, 2015 at 23:34
  • @Suspended have you any idea? Commented May 22, 2015 at 23:35
  • You're not by chance encountering an overflow problem on your last multiplication by 2, are you? Commented May 22, 2015 at 23:40

5 Answers 5

2

There is nothing wrong with your code except int type can only hold so much. Int holds 4 bytes that is 8bitsx4 = 32 bits with the first one reserved for sign, negative if 1 and positive if 0. Try using long int which holds 64 bits:

public static long binaryStringToInteger (String binaryString){
    char[] digits = binaryString.toCharArray();
    long binaryInteger = 0;
    int count = 0;
    for(int i=digits.length-1;i>=0;i--)
      {
        if(digits[i]=='1') 
             {
              binaryInteger +=(long)Math.pow(2, count);
             }
        count++;
      }
     return binaryInteger;
    }

Also check this: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

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

1 Comment

Great if a log was the desired outcome but I thought we were looking for int so a sign would be more appropriate
1
public static long binaryStringToInteger (String binaryString){
char[] digits = binaryString.toCharArray();
long binaryInteger = 0;
long count = 0;
for(int i=digits.length-1;i>=0;i--)
{
    if(digits[i]=='1') 
     {
      binaryInteger +=(long)Math.pow(2, count);
     }
count++;
}
 return binaryInteger;
}

And when converting binaryInteger:

Long.toBinaryString(binaryInteger);

2 Comments

Some explanation would be helpful
I believe it's because of the same reason ppDemo pointed out. He needs long to hold the large value from Math.pow(2, count). Just one less bit in the binary string and his code works fine, that one additional bit takes it over. docs.oracle.com/javase/tutorial/java/nutsandbolts/…
1

You got conversion issues:

2^31 > 2^31-1 = Integer.MAX_VALUE

Therefore the following section from the language specification describes the conversion from double to int:

The value must be too large (a positive value of large magnitude or positive infinity), and the result [...] is the largest representable value of type int [...].

Hence the last value added is 2^31-1 and not 2^31 which causes the issue.

Comments

0

I hope this test result might help. It shows you whats going on quite explicitly.

binStr = 1 int = 1
binStr = 11 int = 3
binStr = 111 int = 7
binStr = 1111 int = 15
binStr = 11111 int = 31
binStr = 111111 int = 63
binStr = 1111111 int = 127
binStr = 11111111 int = 255
binStr = 111111111 int = 511
binStr = 1111111111 int = 1023
binStr = 11111111111 int = 2047
binStr = 111111111111 int = 4095
binStr = 1111111111111 int = 8191
binStr = 11111111111111 int = 16383
binStr = 111111111111111 int = 32767
binStr = 1111111111111111 int = 65535
binStr = 11111111111111111 int = 131071
binStr = 111111111111111111 int = 262143
binStr = 1111111111111111111 int = 524287
binStr = 11111111111111111111 int = 1048575
binStr = 111111111111111111111 int = 2097151
binStr = 1111111111111111111111 int = 4194303
binStr = 11111111111111111111111 int = 8388607
binStr = 111111111111111111111111 int = 16777215
binStr = 1111111111111111111111111 int = 33554431
binStr = 11111111111111111111111111 int = 67108863
binStr = 111111111111111111111111111 int = 134217727
binStr = 1111111111111111111111111111 int = 268435455
binStr = 11111111111111111111111111111 int = 536870911
binStr = 111111111111111111111111111111 int = 1073741823
binStr = 1111111111111111111111111111111 int = 2147483647
binStr = 11111111111111111111111111111111 int = -2
binStr = 111111111111111111111111111111111 int = 2147483645
binStr = 1111111111111111111111111111111111 int = -4

Comments

0

I think that you have a problem with a signed integer value and so should treat the appropriate char (the last I think) a sign bit as follows.

public static int binaryStringToInteger (String binaryString){
    char[] digits = binaryString.toCharArray();
    int binaryInteger = 0;
    int count = 0;
    for(int i=digits.length-2; i>=0;i--)
    {
        if(digits[i]=='1') 
        {
             binaryInteger +=(int)Math.pow(2, count);
        }
        count++;
    }
    if(digits[digits.length-1] =='1') 
    {
        binaryInteger *= -1 ;
    }

    return binaryInteger;
}

Also Integer.toBinaryString () creates an unsigned binary string.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.