Khoi
Khoi

Reputation: 4635

How to overload ~(bitwise not) operator?

I'm trying to use ~ to flip bits of a 'Binary' class. This 'Binary' class stores a char array of '0' and '1's named bs. I just want to flip the characters inside this array:

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

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:

#include <iostream>
#include <windows.h>

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),
      &count,
      NULL
    );

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

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

    // Read the (single) key pressed
    ReadConsole(hstdin, &ch, 1, &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& original) {
            bs = original.bs;
            index = original.index;
        }

        ~Binary() {
            delete [] bs;
            bs = NULL;
        }

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

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

        Binary& operator= (const Binary &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 >> 1);
            if (remainder == 1) {
                bs[index] = '1';
                index += 1;
                return;
            }
            if (remainder == 0) {
                bs[index] = '0';
                index += 1;
                return;
            }
        }

        friend istream& operator>>(istream& is, Binary& b);
        friend ostream& operator<<(ostream& os, Binary& b);
};

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

    is >> str;

    b.bs = str;

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

    return is;
}

ostream& operator<<(ostream& os, Binary& b)
{
    os << b.bs;
    return os;
}

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

    cout << "binary a is " << a << endl;
    cout << "binary b is " << b << endl;
    cout << "binary c is " << c << endl;

    cout << endl << "Re-enter binary b: ";
    cin >> b;

    cout << "binary a is " << a << endl;
    cout << "binary b is " << b << endl;
    cout << "binary c is " << c << endl;

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

    pressanykey();
    return 0;
}

Upvotes: 3

Views: 3560

Answers (1)

alexisdm
alexisdm

Reputation: 29886

You are trying to assign the value with == instead of =.

Upvotes: 6

Related Questions