understanding bitwise operators

could someone please help explain to me the use/purpose of bitwise operators. what is really the point of them, and what is it exactly that they do?
Well, let me give you an example...

Instead of:

1
2
3
4
5
6
7
8
int i = 7;
int x;

if(i == 7) {
    x = 2;
} else {
    x = 3;
}


You could do:

1
2
int i = 7;
int x = (i == 7)?2:3;


That's obviously not the only use. Most bitwise operators are meant for manipulating bits manually. Here's some documentation.

http://www.cplusplus.com/doc/tutorial/operators/
(Bitwise Operators are towards the bottom of the page.)
yeah i was reffering mainly to the operators

<<
>>
&
|
^
~

ill definitely read through that however
Last edited on
Addition and subtraction using only bitwise operators. (An example of how they can be used.)

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
int add(int a, int b) {
	int m = a ^ b,
	n = a & b;
	if( n != 0 )
		return add(m, n << 1);
	else
		return m;
}

int subtract(int a, int b) {
	int m = a ^ b,
	n = ~a & b;
	if( n != 0 )
		return subtract(m, n << 1);
	else
		return m;
}

//----------------------------------------------------------------------------------------------------
#include <iostream>

using namespace std;

int main() {
	int a, b;
	cout << "a = ";
	cin >> a;
	cout << "b = ";
	cin >> b;
	int c = add(a, b), d = subtract(a, b);
	cout << a << " + " << b << " = " << c << endl
		<< a << " - " << b << " = " << d << endl;
	return 0;
}



Another example, using | to combine flags.

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
const int
	NORTH = 1 << 0, //00012  =  1
	SOUTH = 1 << 1, //00102  =  2
	EAST = 1 << 2, //01002  =  4
	WEST = 1 << 3, //10002  =  8

	NORTHEAST = NORTH | EAST, //1 | 4  =  00012 | 01002  =  01012  =  5
	NORTHWEST = NORTH | WEST, //1 | 8  =  00012 | 10002  =  10012  =  9
	SOUTHEAST = SOUTH | EAST, //2 | 4  =  00102 | 01002  =  01102  =  6
	SOUTHWEST = SOUTH | WEST; //2 | 8  =  00102 | 10002  =  10102  =  10

//----------------------------------------------------------------------------------------------------
#include <iostream>

using namespace std;

int main() {
	int dir;
	cout << "dir = ";
	cin >> dir;
	if( (dir & NORTH) == NORTH )
		cout << "You are facing north.\n";
	if( (dir & SOUTH) == SOUTH )
		cout << "You are facing south.\n";
	if( (dir & EAST) == EAST )
		cout << "You are facing east.\n";
	if( (dir & WEST) == WEST )
		cout << "You are facing west.\n";
	return 0;
}
haha shit thats pretty impressive. ill definitely have to figure these out at some point. thank you though ;)
The second example is a pretty common use of bitwise operators. Feel free to ask questions where you don't understand.
So the add & subtract functions are recursive and they keep looping until n = 0. Thats pretty cool. I was wondering if you have any examples of what the bitwise OR (|) is used for. I know how it works but I can't find ways to use it in a program.

Thanks.
Look at my second example above http://cplusplus.com/forum/beginner/40870/#msg220501

Another example:
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
#include <iostream>

using namespace std;


//attributes
const int
	A = 1,  //000012
	B = 2,  //000102
	C = 4,  //001002
	D = 8,  //010002
	E = 16; //100002

int main() {
	int letters = A | C | D; // 011012 == 13
	
	if( (A & letters) == A )
		cout << 'A';
	if( (B & letters) == B )
		cout << 'B';
	if( (C & letters) == C )
		cout << 'C';
	if( (D & letters) == D )
		cout << 'D';
	if( (E & letters) == E )
		cout << 'E';
	cout << endl;

	return 0;
}
ACD
Topic archived. No new replies allowed.