1. Member
Join Date
Nov 2009
Posts
19
Rep Power
0

## 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:

PHP 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,
Last edited by alihht; 02-03-2010 at 03:34 AM.

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,561
Rep Power
11
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.

3. Member
Join Date
Nov 2009
Posts
19
Rep Power
0

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,

4. Member
Join Date
Nov 2007
Location
New Zealand
Posts
36
Rep Power
0
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

Java 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 :-)

5. Member
Join Date
Nov 2009
Posts
19
Rep Power
0
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.

6. Member
Join Date
Nov 2007
Location
New Zealand
Posts
36
Rep Power
0
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.

Java Code:
`binary[reminder] = dec%2;`
You should have
Java 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

7. Member
Join Date
Nov 2009
Posts
19
Rep Power
0
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,
Last edited by alihht; 02-03-2010 at 06:10 AM.

8. Member
Join Date
Nov 2007
Location
New Zealand
Posts
36
Rep Power
0
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.
Last edited by Turtle; 02-03-2010 at 08:43 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•