Unsigned Integer

For a project I have to take in two integers and run all the bitwise operators on them. I got them all to work except for the (~) operator because it makes the number negative than messes things up. I tried to cast the number as an unsigned integer but it still doesn't work. Where am I going wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
using namespace std;
//Overall the purpose of the program is to
//take in two integers (a and b) and use
//the bitwise operators on them. Then print
//out the results in binary, decimal and hexadecimal

//First method will take in an integer and return
//the binary representation
void binary(unsigned int n)
{
    int binaryArray [8] = {0};
    int tempBinary;
    for(int i = 0; n != 0; i++)
    {
        tempBinary = n%2;
        binaryArray[i] = tempBinary;
        n = n/2;
    }
    for(int i = 7; i >= 0; i--){
        cout << binaryArray[i];
    }
}
//This method will take in an integer and
//return the hexadecimal representation
void hexa(unsigned int n)
{
    char hexaArray[8] = {};
    int tempHexa;
    for(int i = 0; n != 0; i++)
    {
        tempHexa = n%16;
        if(tempHexa == 1){
            tempHexa = '1';
        }
        if(tempHexa == 2){
            tempHexa = '2';
        }
        if(tempHexa == 3){
            tempHexa = '3';
        }
        if(tempHexa == 4){
            tempHexa = '4';
        }
        if(tempHexa == 5){
            tempHexa = '5';
        }
        if(tempHexa == 6){
            tempHexa = '6';
        }
        if(tempHexa == 7){
            tempHexa = '7';
        }if(tempHexa == 8){
            tempHexa = '8';
        }
        if(tempHexa == 9){
            tempHexa = '9';
        }
        if(tempHexa == 0){
            tempHexa = '0';
        }
        if(tempHexa == 10){
            tempHexa = 'A';
        }
        if(tempHexa == 11){
            tempHexa = 'B';
        }
        if(tempHexa == 12){
            tempHexa = 'C';
        }
        if(tempHexa == 13){
            tempHexa = 'D';
        }
        if(tempHexa == 14){
            tempHexa = 'E';
        }
        if(tempHexa == 15){
            tempHexa = 'F';
        }
        hexaArray[i] = tempHexa;
        n=n/16;
    }
    for(int i = sizeof(hexaArray)-1; i >=0; i--)
    {
        cout << hexaArray[i];
    }
}
//the main method will execute the bitwise operators
//and print out all the results.
int main(int nNumberofArgs, char* pszArgs[])
{
    unsigned int a;
    unsigned int b;
    cout << "Please enter a value for 'A': ";
    cin >> a;
    cout << "Please enter a value for 'B': ";
    cin >> b;



    int first = a & b;
    int n = first;
    cout << a << " & " << b << "  =  " << first << "(base10)  ";
    binary(n);
    cout << "(base2)  ";
    hexa(n);
    cout << "(base16)";
    cout << endl;

    int second = a | b;
    n = second;
    cout << a << " | " << b << "  =  " << second << "(base10)  ";
    binary(n);
    cout << "(base2)  ";
    hexa(n);
    cout << "(base16)";
    cout << endl;

    int third = a ^ b;
    n = third;
    cout << a << " ^ " << b << "  =  " << third << "(base10)  ";
    binary(n);
    cout << "(base2)  ";
    hexa(n);
    cout << "(base16)";
    cout << endl;

    int fourth = ~(a );
    n = fourth;
    cout << "~" << a << "     =  " << fourth << "(base10)  ";
    binary(n);
    cout << "(base2)  ";
    hexa(n);
    cout << "(base16)";
    cout << endl;

    int right = a >> b;
    n = right;
    cout << a << " >> " << b << " =  " << right << "(base10)  ";
    binary(n);
    cout << "(base2)  ";
    hexa(n);
    cout << "(base16)";
    cout << endl;

    int left = a << b;
    n = left;
    cout << a << " << " << b << " =  " << left << "(base10)  ";
    binary(n);
    cout << "(base2)  ";
    hexa(n);
    cout << "(base16)";
    cout << endl;
}


Any info would be great!
Make 'fourth' unsigned. It doesn't matter what 'a' is, because you're printing 'fourth'. If you're not aware of how negative numbers work, google two's complement.
Topic archived. No new replies allowed.