Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
The method I need to create is: public static String IntToBinary(int inputInt).

This method should take as input a single int, convert this int to a binary version of itself (e.g. 3 becomes “11”) and output that binary number as a string.

I have written some code but it honestly doesn't do what the question says i need to do.

What I have tried:

//This is the only way i know how to convert an it doesn't do what i need it to do in the question as i have to insert a single integer convert it to the binary and then make it output the string the method below doesn't do this

static void usingArray() {

int binary[] = new int[20];

int decimalNumber = 25;
int index = 0;

while (decimalNumber > 0) {

int remainder = decimalNumber % 2;

binary[index++] = remainder;


decimalNumber = decimalNumber / 2;
}
//loop over the array backwards and print the binary number
for (int i = index - 1; i >= 0; i--) {
System.out.print(binary[i]);
}
}
Posted
Updated 12-Apr-19 0:32am
Comments
Richard MacCutchan 12-Apr-19 6:11am    
What does it do?
KAID6IX 12-Apr-19 6:28am    
Well for a start it does not output the binary as a string.
im also not sure if there is a faster way to input a single integer instead of using an array.

1 solution

There are several ways to do this in Java:
1) Use the framework: Java lang.Integer.toBinaryString() method - GeeksforGeeks[^]
2) Do it yourself.
The first is trivial, and probably not allowed for your homework!

The second means you need to read the question more carefully.
It specifically asks you to create a method that accepts an integer parameter and returns a string: your's doesn't do that. So start by getting the method signature right:
public static String ToBinary(int x)
{
return ...
}
Which means that instead of an array of integers, you need a string to "build into".

I'd start with two loops: one to find the most significant non-zero bit then one to convert the rest. That way, you don't have to "reverse" anything, which makes the task simpler.

Think about it, and then see how far you can get!
 
Share this answer
 
Comments
KAID6IX 12-Apr-19 7:16am    
Yeah, I have searched everywhere for some sort of answer or example and I did come across the first link you attached but I cant use that.

Can you please explain what you meant by using a loop to find the most significant no zero bit, specifically the non-zero bit.
OriginalGriff 12-Apr-19 7:34am    
An integer is a 32 bit number: and the "Most significant" bit is the highest.
Since you want to only show 5 bits for "25": 11001 there are 27 bits you aren;t interested in:
00000000000000000000000000011001
So write a simple loop that runs from 31 to 0 inclusive and find the highest bit position that contains a "1" bit.
If you set a mask to contain a single bit in the highest position:
10000000000000000000000000000000
You can use the Java Shift operators to move it through the bits, and use teh And operator to check if it's zero or one.

http://www.iitk.ac.in/esc101/05Aug/tutorial/java/nutsandbolts/bitwise.html

Find the index of the highest bit, and you exit the loop, and use that to start your second loop.
Think about it for a minute, and you'll get what I mean.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900