Results 1 to 8 of 8
  1. #1
    alihht is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default 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. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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. #3
    alihht is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default

    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,

  4. #4
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

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

    Default

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

    Default

    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
    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. #7
    alihht is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default

    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. #8
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    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.

Similar Threads

  1. Bit Wise operation: shifting
    By rueter in forum New To Java
    Replies: 1
    Last Post: 06-27-2009, 06:51 AM
  2. Shifting characters in array
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 04-24-2009, 10:19 PM
  3. [SOLVED] Shifting an array
    By VeasMKII in forum New To Java
    Replies: 2
    Last Post: 02-04-2009, 06:18 PM
  4. add data into an array
    By mispeed in forum New To Java
    Replies: 9
    Last Post: 11-08-2007, 03:53 AM
  5. Add data to an array
    By adlb1300 in forum New To Java
    Replies: 8
    Last Post: 11-05-2007, 02:01 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
  •