I am just trying to do the bitwise complement in C++ with ~ operator:
For example:
NOT 0101
--------
    1010
So in the following code, I was expecting to get 1010 but I am getting negative numbers. How is it possible although I define the values with unsigned types?
#include <iostream>
#include <stdio.h>
#include <string>
#include <bitset>
using namespace std;
int tob(int num) {
    if (num == 0)
        return 0;
    return (num % 2) + 10*tob(num/2);
}
long unsigned int tol(string st) {
    long unsigned int num = bitset<100>(st).to_ulong();
    return num;
}
int main()
{
    unsigned int x = tol("0101");
    unsigned int z = ~x;
    printf("%10d (decimal %u)\n", tob(z), z);
    // -110 (decimal -6)
    cout << tob(z) << endl; // -110
    cout << z << endl; // -110
}
And how do I get 1010 from not 0101 in C++?
Thanks!
 
    