# Q about shifting data in an array

• 02-03-2010, 04:31 AM
alihht
Q about shifting data in an array
Hi,

I'm trying to make a program to convert from decimal to binary using the array. The problem is the result will appear like this :

the decimal number is : 43 ( for example)
After the conversion : 1 1 0 1 0 1

The result is wrong, it must be : 101011

How can I resort the array to be correct.

This is my code:

Code:

import java.util.*;

public class TestL
{
public static void main (String[] args)
{

int[] binary = new int[8];
int temp;
int reminder = 0;
int tempVar;

Scanner keyboard = new Scanner (System.in);
int dec = keyboard.nextInt();

do
{

temp = dec / 2;

binary[reminder] = dec%2;

System.out.print(binary[reminder] + " ");
System.arraycopy(binary, 1, binary, 0, binary.length-1);

dec = temp;

}while (temp  > 0) ;

}
}

Regards,
• 02-03-2010, 04:39 AM
pbrockway2
It looks like you are printing the binary digits backwards ie from the 1's place first and then higher valued digits.

Notice that at the moment you are printing inside the do loop. And that loop figures out the binary digits from the right hand end. If you want to print the binary digits in their "normal" order you will have to move that println() statement outside the loop. Only after the loop is finished and you have filled the binary array will you be able to print it.
• 02-03-2010, 04:51 AM
alihht

Does that mean that I have to make another array outside the loop to copy the first one?

because if there is no println() in side the loop the results will not be stored.

Actually, I'm just a beginner so make the explanation clear for me, Please.

Regards,
• 02-03-2010, 05:08 AM
Turtle
Doing it on paper first helps.

Example: 48

48 / 2 = 24 r 0
24 / 2 = 12 r 0
12 / 2 = 6 r 0
6 / 2 = 3 r 0
3 / 2 = 1 r 1
1 / 2 = 0 r 1

000011 reversed is 110000.
Then fill the front with leading zeros = 0011 0000
Check this binary value: 1*(2^5) + 1*(2^4) = 32 + 16 = 48

Code:

public class TestL {
public static void main (String[] args) {
Scanner keyboard = new Scanner(System.in);
StringBuffer binaryString = new StringBuffer(8);
System.out.print("Enter a decimal value: ");

int decimal = keyboard.nextInt();
int remainder;

do {
remainder = decimal % 2;
binaryString.append(remainder);
decimal /= 2;
} while (decimal > 0);

while(binaryString.length() < 8) {
binaryString.append(0);
}

binaryString = binaryString.reverse();
System.out.println("Binary value: " + binaryString);
}
}

I'm sure you will be able to convert this code to use an array if you need to :-)
• 02-03-2010, 05:44 AM
alihht
Thank you man, but I think you understand me wrong.

I don't want the solution, I want to understand.

What you use is new for me:

1- I don't know what does StringBuffer class do?!!!!
2- I don't know how to use the methods :
A- append.
B- reverse.

If you want me to use them and convert the code to an array, I will ask you first to explain what are those?? and when and where I have to use them.

"I'm Just a beginner and I want somebody to explain how can I sort the array that I made"

I don't want you to solve it. Just teach me how can I do it.

I wanna do it by myself.
• 02-03-2010, 06:07 AM
Turtle
My apologies alihht,

StringBuffer is a dynamic string. It's length is not fixed.
The append method adds to the end of it.
The reverse method reverses it.

I hoped that this would let you understand that you need to place the remainder at the end, and when you have finished you need to reverse the result.

You're original code fails because you are not storing the remainder correctly and you are not reversing afterward.

Code:

binary[reminder] = dec%2;
You should have
Code:

int index = 0;

//... inside loop...
remainder = dec % 2;
binary[index] = remainder;
index++;

//... after loop...
// reverse the array

I am happy to illustrate this with code.

Respectfully
• 02-03-2010, 07:04 AM
alihht
Thank you for helping me.

I'm thinking about algorithm of the reversing idea:

I need a (temp) Var to store the indexOf(0)
That will remain just 7 items stored
I need to shift the 7 items backward by 1.
I need to assign the temp to the last index of the array which is (8).
I need two loops :

the first one to decrease the number of the data which must be shifted and do the shifting process.
the second one to store the items from the end.

ex :

110101
101011
010111
101011
011011
101011
101011

Am I far a way from the correct sorting?

Regards,
• 02-03-2010, 07:17 AM
Turtle
Your idea is good. You actually just need one loop. Swapping the front with the back until you reach the middle. This is easy code to write, so I'll let you do it and not spoil your fun.