Working with Binary Numbers in C++

Hi everyone,

I'm having problems getting a flipped binary number (one where all the 1's of a number become 0's and vice versa) to display correctly. My program converts a value from decimal to binary and then should flip the converted binary number so that all 1's become 0's and 0's become 1's. It is not displaying the flipped number properly. I was wondering how I can get it to be displayed so that 1's become 0's and 0's become 1's. I tried using the stndard tilde symbol for accomplishing this but it didn't work. Now I'm stuck as to why it won't work.

Here is my code for this program so far:

Code:

#include <iostream>

#include <stdlib.h>

#include <conio.h>

#include <string>

#include <bitset>

using namespace std;

class BitHandler {

private:

unsigned int value;

public:

void print();

long binary(int);

int binflip(int);

};

void BitHandler::print() {

value = 128;

cout << "Decimal Value: " << value << endl;

cout << "Binary Value: " << binary(value) << endl;

cout << "Flipped Value: " << binflip(value) << endl;

}

long BitHandler::binary(int value) { // converts to binary

int rem;

long x = 0;

if (value == 0) {

return 0;

}

rem = value % 2;

value /= 2;

x = binary(value) * 10 + rem;

return x;

}

int BitHandler::binflip(int value) { // converts to binary

int rem;

int y;

long x = 0;

if (value == 0) {

return 0;

}

rem = value % 2;

value /= 2;

x = binary(value) * 10 + rem;

y = ~x; // this line should flip the binary number to it's inverse

return y;

}

int main() {

BitHandler bits;

bits.print();

getche();

return 0;

}

Any help or suggestions would be greatly appreciated. Thanks.