how to overload ~(bitwise not) operator?
<p>I'm trying to use ~ to flip bits of a 'Binary' class. So this 'Binary' class stores a char array of '0' and '1' named <code>bs</code>. I just want to flip the characters inside this array:</p>

<pre><code>Binary&amp; operator~ () 
    {
        int i = 0;
        while( i &lt; index ) {
            if (bs[i] == '1')
                bs[i] == '0';
            else bs[i] == '1';
            i++;
        }
        return *this;
    }
</code></pre>

<p>but the code seem unable to enter loop no matter if I use while or for. Maybe I have written it wrong. Here's the whole code:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;windows.h&gt;

using namespace std;

TCHAR pressanykey( const TCHAR* prompt = NULL )
  {
  TCHAR  ch;
  DWORD  mode;
  DWORD  count;
  HANDLE hstdin = GetStdHandle( STD_INPUT_HANDLE );

  // Prompt the user
  if (prompt == NULL) prompt = TEXT( "Press any key to continue..." );
  WriteConsole(
    GetStdHandle( STD_OUTPUT_HANDLE ),
    prompt,
    lstrlen( prompt ),
    &amp;count,
    NULL
    );

  // Switch to raw mode
  GetConsoleMode( hstdin, &amp;mode );
  SetConsoleMode( hstdin, 0 );

  // Wait for the user's response
  WaitForSingleObject( hstdin, INFINITE );

  // Read the (single) key pressed
  ReadConsole( hstdin, &amp;ch, 1, &amp;count, NULL );

  // Restore the console to its previous state
  SetConsoleMode( hstdin, mode );

  // Return the key code
  return ch;
  }

class Binary {
    char *bs;
    int index;
public:
    Binary(int x) {            
        bs = new char[20];
        index = 0;
        DecimalToBinary(x);
        bs[index] = '\0';
    }
    Binary(char* str) {
        bs = str;
        index = 0;
        while (bs[index] != '\0') {
            index += 1;
        }
    }
    Binary(const Binary&amp; original) {
        bs = original.bs;
        index = original.index;
    }
    ~Binary() {
        delete [] bs;
        bs = NULL;
    }

    int ToDecimal() {
        int result = 0;
        for (int i = 0; i &lt; index; i++) {
            result *= 2;
            if (bs[i] == '1')
                result += 1;
        }
        return result;
    }

    Binary&amp; operator~ () 
    {
        int i = 0;
        while( i &lt; index ) {
            if (bs[i] == '1')
                bs[i] == '0';
            else bs[i] == '1';
            i++;
        }
        return *this;
    }

    Binary&amp; operator= (const Binary &amp;b) {
        delete [] bs;
        bs = NULL;

        bs = b.bs;
        index = b.index;

        return *this;
    }


    void DecimalToBinary(int number) {
        int remainder;

        if(number == 1) {
            bs[index] = '1';
            index += 1;
            return;
        }
        if(number == 0) {
            bs[index] = '0';
            index += 1;
            return;
        }

        remainder = number%2;
        DecimalToBinary(number &gt;&gt; 1);    
        if(remainder == 1) {
            bs[index] = '1';
            index += 1;
            return;
        }
        if(remainder == 0) {
            bs[index] = '0';
            index += 1;
            return;
        }
    }

    friend istream&amp; operator&gt;&gt;(istream&amp; is, Binary&amp; b); 
    friend ostream&amp; operator&lt;&lt;(ostream&amp; os, Binary&amp; b);
};

istream&amp; operator&gt;&gt;(istream&amp; is, Binary&amp; b)
{
    char *str = new char[20];

    is &gt;&gt; str;

    b.bs = str;

    b.index = 0;
    while (b.bs[b.index] != '\0') {
        b.index += 1;
    }

    return is;
}
ostream&amp; operator&lt;&lt;(ostream&amp; os, Binary&amp; b)
{
    os &lt;&lt; b.bs;
    return os;
}

int main() {
    Binary a(15);
    Binary b("10110");
    Binary c(b);

    cout &lt;&lt; "binary a is " &lt;&lt; a &lt;&lt; endl;
    cout &lt;&lt; "binary b is " &lt;&lt; b &lt;&lt; endl;
    cout &lt;&lt; "binary c is " &lt;&lt; c &lt;&lt; endl;

    cout &lt;&lt; endl &lt;&lt; "Re-enter binary b: ";
    cin &gt;&gt; b;

    cout &lt;&lt; "binary a is " &lt;&lt; a &lt;&lt; endl;
    cout &lt;&lt; "binary b is " &lt;&lt; b &lt;&lt; endl;
    cout &lt;&lt; "binary c is " &lt;&lt; c &lt;&lt; endl;

    cout &lt;&lt; "binary b in decimal form: " &lt;&lt; b.ToDecimal() &lt;&lt; endl;
    cout &lt;&lt; "bit flips b: "&lt;&lt; ~b &lt;&lt; endl;

    pressanykey();
    return 0;
}
</code></pre>
