Bitwise Operators

Apr 27, 2010 at 7:40pm
Hello Everyone!

I am currently working on a bitwise operators program. I am trying to display two numbers that the user enters in both, base 2 and base 10, the bitwise AND, the bitwise OR, and the bitwise EXCLUSIVE OR. However I don't think I am converting to base 2 correctly, and I keep getting 0 for all of the base 2 calculations. Any help with this problem would be greatly appreciated! 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
// 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(int num);

int main()
{
	short unsigned int num1, num2, num3, num4;
	short unsigned int answer = 0;

	cout << "Please enter your first value --> ";
	cin >> num1;
	cout << "Please enter your second value --> ";
	cin >> num2;
	
	answer = num1 & num2;
	cout << "The bitwise AND in base 10 for your two values is --> ";
	cout << answer << ".\n";
	
	answer = num1 | num2;
	cout << "The bitwise OR in base 10 for your two values is --> ";
	cout << answer << ".\n";

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

	num3 = convert(num1);
	num4 = convert(num2);

	cout << "I have now converted your values into base 2!\n\n";
	
	answer = num3 & num4;
	cout << "The bitwise AND in base 2 for your two values is --> ";
	cout << answer << ".\n";

	answer = num3 | num4;
	cout << "The bitwise OR in base 2 for your two values is --> ";
	cout << answer << ".\n";
	
	answer = num3 ^ num4;
	cout << "The bitwise EXCLUSIVE OR in base 2 for your two values is --> ";
	cout << answer << ".\n";

	cout << endl << endl;



}

int convert(int num)
{
	if(num<0) {
		convert(num/2);
		cout << num % 2;
	}
	return 0;
}
Apr 27, 2010 at 7:54pm
1
2
3
4
5
6
7
8
int convert(int num)
{
	if(num<0) {
		convert(num/2);
		cout << num % 2;
	}
	return 0;
}


This function always returns zero. Therefore doing something like this doesn't work:

1
2
num3 = convert(num1);  // since 'convert' always returns zero, this means that num3
   // will be zero no matter what 'num1' was. 


All your "convert" function does is print a number to the screen via cout (and it even does that improperly -- your 'if' statement only prints if the number is less than 0, which I'm assuming is never the case)


Furthermore... there's nothing to convert here. The computer treats all numbers are binary so there's no conversion that needs to be done.
Apr 27, 2010 at 8:14pm
Ok so then how would I do the bitwise AND, OR, and EXCLUSIVE OR, for two numbers in base 2?
Last edited on Apr 27, 2010 at 8:18pm
Apr 27, 2010 at 9:25pm
Just like normal:

1
2
3
4
int x = 10;
int y = 60;

int z = x | y;  // bitwise OR 
Apr 28, 2010 at 12:12am
So then, to display the bitwise OR in base 2 is the same as displaying it in base 10??
Apr 28, 2010 at 2:49am
no if you want to display them in base 2, then that takes some "converting".

I suggested showbase() in that other thread, but after testing it it doesn't appear to work, so here's this instead:

1
2
3
4
5
6
7
8
void base2print(unsigned v)
{
  // recursion solution.  Not the best, but I'm lazy
  if(v > 1)
    base2print(v>>1);

  cout << (v&1);
}


The thing is, an integer just holds a number. The number isn't necessarily in base 2 or base 10 or base anything... it's just a number. Fourteen is still the number fourteen no matter how you display it.

But when you display it you're converting a number (14) to a string (the characters '1' and '4' if base ten... or '1','1','1','0' if base 2), so that is where the conversion is done. number->string, not number->number.
Apr 28, 2010 at 2:34pm
I understand how to "convert" and number to such as 14 to base 2 and also how to program it and display it. However, I am still uncertain on how to display the bitwise OR, AND, and EXCLUSIVE OR for numbers in both base 10 and base 2. How would I do this? The way I have it in the program that I submitted above in the first post, is that not displaying it in base 10 first, and then I attempt to display it in base 2? Or, am I already displaying it in base 2 in the first place, and then somehow have to attempt to display it in base 10 later? Sorry if this post is a little hard to understand...
Apr 28, 2010 at 2:54pm
I understand how to "convert" and number to such as 14 to base 2 and also how to program it and display it. However, I am still uncertain on how to display the bitwise OR, AND, and EXCLUSIVE OR for numbers in both base 10 and base 2. How would I do this?


This doesn't really make sense. If you really understand what you say in the first sentence, then you have the answer to the second sentence.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// assume you have the above 'base2print' function which takes a number
//   and prints it as base 2.

int x = 14;

// x is now == fourteen.  The number fourteen is the number fourteen, no matter how you display it.
//  whether it be displayed as "14", "1110" or "fourteen", or whatever, it's still the same number.
//  therefore 'x' does not need to be changed.  The only thing that needs to be changed is
//  the character string that you output.

// for example:
cout << x;  // outputs as base 10 (prints "14")
base2print(x);  // outputs as base 2 (prints "1110")

// now let's say we want to do 14 & 3:

x &= 3;

// x is now == two (because 14 & 3 == 2).  Again this is just a number and the number doesn't need to
//  to be converted to or from any base.  Numbers are numbers

// to print it:
cout << x;  // output as base 10  (prints "2")
base2print(x);  // output as base 2 (prints "10") 
Apr 28, 2010 at 6:58pm
I understand now...the problem that I was working on was a little confusing in the way it was worded. I realize now that the answer that I got from the bitwise OR, AND, and EXCLUSIVE OR is supposed to be printed out in both base 10 and base 2. Thanks for all of your help by the way!
Topic archived. No new replies allowed.