Results 1 to 8 of 8
- 02-03-2010, 03:31 AM #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:
Regards,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) ; } }Last edited by alihht; 02-03-2010 at 03:34 AM.
- 02-03-2010, 03:39 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.
- 02-03-2010, 03:51 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 19
- Rep Power
- 0
Thank you for your reply:
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, 04:08 AM #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
I'm sure you will be able to convert this code to use an array if you need to :-)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); } }
- 02-03-2010, 04:44 AM #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.
- 02-03-2010, 05:07 AM #6
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
My apologies alihht,
I had hoped that the code would aid in your understanding.
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.
Instead of
You should haveJava Code:binary[reminder] = dec%2;
I am happy to illustrate this with code.Java Code:int index = 0; //... inside loop... remainder = dec % 2; binary[index] = remainder; index++; //... after loop... // reverse the array
Respectfully
- 02-03-2010, 06:04 AM #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.
- 02-03-2010, 06:17 AM #8
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
Similar Threads
-
Bit Wise operation: shifting
By rueter in forum New To JavaReplies: 1Last Post: 06-27-2009, 06:51 AM -
Shifting characters in array
By Mayur in forum New To JavaReplies: 2Last Post: 04-24-2009, 10:19 PM -
[SOLVED] Shifting an array
By VeasMKII in forum New To JavaReplies: 2Last Post: 02-04-2009, 06:18 PM -
add data into an array
By mispeed in forum New To JavaReplies: 9Last Post: 11-08-2007, 03:53 AM -
Add data to an array
By adlb1300 in forum New To JavaReplies: 8Last Post: 11-05-2007, 02:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks