Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Swapping 0 and 1s in an array

Posted on 2004-09-28
Medium Priority
276 Views
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
Question by:NeedlessKane
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3
• 2

LVL 92

Accepted Solution

objects earned 200 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

ID: 12175001
That's OK in principle, but why break?
0

LVL 86

Expert Comment

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

Author Comment

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

LVL 86

Expert Comment

ID: 12175132
The code i posted is not only more efficient on your processor, but more efficient on your wrists ;-)
0

LVL 92

Expert Comment

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

Author Comment

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

LVL 92

Expert Comment

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

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

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing instaâ€¦
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arrâ€¦
###### Suggested Courses
Course of the Month11 days, 10 hours left to enroll