1

I have a program whose intention is to take in a decimal integer and return the binary value.

  public static int returnBinary(int number) {
            int current;
            int digit = 1;
            int result = 0;
            while (number > 0) {
                current = number % 2;
                number = number/2;
                result = result + current * digit;
                digit = digit * 10;
            }
            return result;
        }

This program works perfectly until it reaches the value 1024, for which it returns the value: 1410065408

Larger values than 1024 also do not work. I noticed 1023 in binary is 1111111111, which may be relevant as to why 1024 does not work.

11
  • 2
    try returning your result as a String, instead of an int. Commented Jan 31, 2018 at 14:27
  • 4
    So result is a decimal int but containing only 0 and 1? Maximum value for int is about 2.1 billion - you're likely exceeding that. Anything wrong with Integer.toBinaryString. Commented Jan 31, 2018 at 14:30
  • Got it, I would have to use long. Thanks. Commented Jan 31, 2018 at 14:32
  • Don't use long, use a data structure that can actually represent a reasonably-sized binary array (such as string, List<Boolean>, or Boolean[]). Commented Jan 31, 2018 at 14:33
  • 8
    No, don't use long - use a string. You're trying to return the textual representation of a number in a particular base. The way to return text is to use a string. Commented Jan 31, 2018 at 14:34

3 Answers 3

3

You are trying to assign a value which is higher than the Integer.MAX_VALUE. In Java it is 2.147.483.647 and the binary value of 1.111.111.111 might be 1023 in decimal, but it is around half of the Integer.MAX_VALUE. Anything higher is not representable in Integer. You might want to return a String instead.

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

Comments

0

You can return String rather than the integer, please find the string implementation below.

public static String returnBinary(int number) {
    String result = "";
    int digit;
    while (number > 0) {
        digit = number & 0X1;
        result = digit+result;
        number = number >> 1;
    }
    return result;
}

1 Comment

This answer is correct in the terms of implementing the solution for homework (otherwise, the comment on the question to use toBinaryString() is likely best). So, given that this is in the context of homework, you might not want to just outright solve it for them.
-1

In binary 1024 is 10000000000 however max value for an int is 2 147 483 647 so you have exceeded the maximum size

2 Comments

The compiler reacts strangely? This is both vague and wrong.
It is not compiler that reacts, and reaction is not strange, it is simple integer overflow which happens at runtime.

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.