Bitwise Operators

Hello Everyone! I am currently working on a program that requires me to ask the user to input two numbers and then displays those numbers bitwise AND, OR, and EXCLUSIVE OR. I also need to put in an exception handler for negative values. However, when I put in the exception into the program it does not seem to be working as is because everytime I enter a negative value the program continues to run without "throwing" a message. Any insight into what I am doing wrong with the exception would be great! Here is what I have so far:


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
// This program prompts the user to enter any two, short unsigned
// integer values, then displays, in both base 2 and base 10,
// the bitwise AND, OR, and EXCLUSIVE OR, of the two values.

#include <iostream>

using namespace std;

int convert(unsigned short int num);
int checkVals(unsigned short int &num1, unsigned short int &num2);
int fixNUMS(unsigned short int &num1, unsigned short int &num2);

int main()
{
	short unsigned int num1, num2, num3, num4, num5;
	
	try {
	cout << "Please enter your first value --> ";
	cin >> num1;
	cout << "Please enter your second value --> ";
	cin >> num2;
	checkVals(num1, num2);
	}
	catch (char *exceptionString) {
		cout << exceptionString << endl;
		fixNUMS(num1, num2);
	}

	cout << endl << endl;
	
	num3 = num1 & num2;
	cout << "The bitwise AND in base 10 for your two values is --> ";
	cout << num3 << ".\n";

	cout << "The bitwise AND in base 2 for your two values is --> ";
	cout << convert(num3) << ".\n";
	
	num4 = num1 | num2;
	cout << "The bitwise OR in base 10 for your two values is --> ";
	cout << num4 << ".\n";

	cout << "The bitwise OR in base 2 for your two values is --> ";
	cout << convert(num4) << ".\n";

	num5 = num1 ^ num2;
	cout << "The bitwise EXCLUSIVE OR in base 10 for your two values is --> ";
	cout << num5 << ".\n";
	
	cout << "The bitwise EXCLUSIVE OR in base 2 for your two values is --> ";
	cout << convert(num5) << ".\n";

	cout << endl << endl;
}

int convert(unsigned short int num)
{
	if(num>0) {
		convert(num/2);
		cout << num % 2;
	}
	return 0;
}

int checkVals(unsigned short int &num1, unsigned short int &num2)
{
	if ((num1 < 0) || ((num2 < 0))
		throw "Error - one(or both) of your value(s) is negative!\n";
	else
		return num1, num2;
}

int fixNUMS(unsigned short int &num1, unsigned short int &num2)
{
	if ((num1 < 0) && (num2 < 0)) {
		while ((num1 < 0) && (num2 < 0)) {
			cout << "Both of your values are negative!!\n";
			cout << "They need to be positive!!\n";
			cout << "Please reenter value # 1 --> ";
			cin >> num1;
			cout << "Please renter value # 2 --> ";
			cin >> num2;
		}
		return num1, num2;
	}
	else if (num1 < 0) {
		while (num1 < 0) {
			cout << "Value # 1 is negative!\n";
			cout << "It needs to be positive!!\n";
			cout << "Please reenter your value now --> ";
			cin >> num1;
		}
		return num1, num2;	
	}
	else {
		while (num2 < 0) {
			cout << "Value # 2 is negative!\n";
			cout << "It needs to be positive!!\n";
			cout << "Please reenter your value now --> ";
			cin >> num2;
		}
		return num1, num2;
	}
}
Unsigned values cannot, by definition, be negative.
Ok, so is that why it isn't even throwing a message when they I enter a negative number??
Wow, I guess that is why it wasn't throwing a message, because when I changed all of the values to regular integers it finally threw a message. Thanks for the help!
Topic archived. No new replies allowed.