Solved

Swapping 0 and 1s in an array

Posted on 2004-09-28
10
223 Views
Last Modified: 2010-03-31
Hello,

For this assignment i'm doing for class(an interative towers of hanoi), I have an array that will contain 0 and 1s that correspond to the number of the disk that is to be moved. And my question is this:

That array, will initially be intitialized to all 1s, and then it needs to swap all 0s to 1s, and all 1s to 0s, but I can't figure out how to do it. I wrote a method but it just undoes work that it's already done, thus giving me what i start with.

here it is:
public static void  bitSwap(int[] swapArray)
{

for (k=0; k < swapArray.length; k++)
{

       if (swapArray[k] == 0)
              {
                    swapArray[k] = 1;
                    break;
               }
                  
}


for (j=0; j < swapArray.length; j++)
{      
            
            if  (swapArray[j] == 1)
               {      

      swapArray[j] = 0;
      break;
      
                }      


}      
}

As you can see, that does undoes itself.  I know it's EASY but i can't figure it out, any help would be appreciated.

-NeedlessKane
0
Comment
Question by:NeedlessKane
  • 5
  • 3
  • 2
10 Comments
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 12175000
try it in one loop:

for (k=0; k < swapArray.length; k++)
{

       if (swapArray[k] == 0)
       {
             swapArray[k] = 1;
       }
       else
       {
             swapArray[k] = 0;
       }              
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12175001
That's OK in principle, but why break?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12175015
for (k=0; k < swapArray.length; k++)
{
      swapArray[k] ^= 1;
}
0
 

Author Comment

by:NeedlessKane
ID: 12175099
Ok, Object thanks. lol I can't believe i didn't do that, wow. So easy.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12175132
The code i posted is not only more efficient on your processor, but more efficient on your wrists ;-)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 92

Expert Comment

by:objects
ID: 12175142
No worries, glad I could help :)
Let me know if you have any further questions.
0
 

Author Comment

by:NeedlessKane
ID: 12175170
CEHJ: i tried yours, it's not working for me.  What does the ^= operator do?
0
 
LVL 92

Expert Comment

by:objects
ID: 12175210
> i tried yours, it's not working for me.

So much for effiency on your wrist and processor :-D

> What does the ^= operator do?

Not whats required here ;)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12175214
>>CEHJ: i tried yours, it's not working for me.  

Why not?

>>What does the ^= operator do?

Performs a bitwise operation on the number to turn 1s into 0s and 0s into 1s
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12175289
My code works perfectly well, as the following shows:

            int[] swapArray = new int[10];
            for (int k=0; k < swapArray.length; k++) {
                  swapArray[k] = Math.random() > 0.5? 1: 0;
            }

            // Before
            System.out.println(MyArrays.toString(swapArray));
            for (int k=0; k < swapArray.length; k++)
            {
                        swapArray[k] ^= 1;
            }
            // After
            System.out.println(MyArrays.toString(swapArray));


      ....
      

                  static class MyArrays {
                        public static String toString(int[] array) {
                              StringBuffer sb = new StringBuffer();
                              sb.append("[");
                              for (int k=0; k < array.length - 1; k++) {
                                    sb.append(array[k]).append(", ");
                              }
                              sb.append(array[array.length - 1]).append("]");
                              return sb.toString();
                        }
                  }



Producing when i ran it:

[0, 1, 1, 0, 0, 0, 1, 0, 0, 1]
[1, 0, 0, 1, 1, 1, 0, 1, 1, 0]
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countAbc challenge 9 49
Java asynchronous logging 4 34
recursion example 16 68
backtracking recursion  code 19 40
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now