The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

DECtoBIN Conversion WITHOUT using String Methods

Hey I am trying to create a programme that determines the binary sequence of an input (unknown) decimal number. I DO NOT want to use a String Method.

I am running into the difficulty that my remainder is stuck on an endless loop. I want to find the divisible value of the decimal number (ie the quotient) and it's remainder, then print the remainder. Then continue calculating the divisible value of the quotient and its subsequent remainder until the quotient is 0.

For example the decimal number 10 should produce a binary sequence 1010.

Re: DECtoBIN Conversion WITHOUT using String Methods

decimal number 10 should produce a binary sequence 1010.

What type of variable is that "binary sequence" held in? Do you need to drop the leading 0s?

An int variable holds 32 bits that can be shifted bit by bit. any bit of the int can be tested by ANDing the bit with a 1 in and testing if the results is 0. For example: 1010 AND 1000 = 1000 or 1101 AND 1 = 1

Re: DECtoBIN Conversion WITHOUT using String Methods

The variable that the binary sequence is held in is an int variable.

If the input is in an int variable and the binary sequence is held in an int variable, I don't understand what the code is supposed to do. There wouldn't be any logic needed, just an assignment statement:
int inV = 10;
int binarySeq = inV; // copy the value here

Did you really mean that or did you mean that "binary sequence" will be printed as digits on the console?