Bitwise operations

I'm struggling a bit with this, is there any easily understandable way of explaining bitwise operations to a beginner? I'm using the book Sam's Teach Yourself C++ in One Hour a Day and still can't figure it out properly :/

Thanks in advance.
You are struggling a bit? :-) Ok, I could not resist...

Anyway, this tutorial seems pretty good: http://www.cprogramming.com/tutorial/bitwise_operators.html
Do you understand how numbers are represented in base 2? Once you get that, the rest will fall into place very quickly.
I know @minomic, I also noticed that right away after writing the sentence and thought it was funny :p

What do you mean by base 2 @dhayden? You mean shifting? I understand that, >> divides and << does multiplications and the numbers used in the shifting are multiples of 2. That's made by moving the most significant bit to the right or to the left.
1
2
int calc = 8 >> 2;
cout << calc; // result = 2 


I actually understand the concept of bitwise operations, my problem here is mostly about how are they used ("why", I already know why they're useful but I don't get how to actually use them). I already know how to convert "normal" integers to bits and I get all those 1's and 0's which have a meaning, what I want to know is how am I gonna use them.

Here is the example with the output in the book I'm using:
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
 #include <iostream>
 #include <bitset>
 using namespace std;

 int main()
 {
 cout << “Enter a number (0 - 255): “;
 unsigned short InputNum = 0;
 cin >> InputNum;

 bitset<8> InputBits (InputNum);
 cout << InputNum << “ in binary is “ << InputBits << endl;

 bitset<8> BitwiseNOT = (~InputNum);
 cout << “Logical NOT |” << endl;
 cout << “~” << InputBits << “ = “ << BitwiseNOT << endl;

 cout << “Logical AND, & with 00001111” << endl;
 bitset<8> BitwiseAND = (0x0F & InputNum);// 0x0F is hex for 0001111
 cout << “0001111 & “ << InputBits << “ = “ << BitwiseAND << endl;

 cout << “Logical OR, | with 00001111” << endl;
 bitset<8> BitwiseOR = (0x0F | InputNum);
 cout << “00001111 | “ << InputBits << “ = “ << BitwiseOR << endl;

 cout << “Logical XOR, ^ with 00001111” << endl;
 bitset<8> BitwiseXOR = (0x0F ^ InputNum);
 cout << “00001111 ^ “ << InputBits << “ = “ << BitwiseXOR << endl;

return 0;
 }


Output:
Enter a number (0 - 255): 181
181 in binary is 10110101
Logical NOT |
~10110101 = 01001010
Logical AND, & with 00001111
0001111 & 10110101 = 00000101
Logical OR, | with 00001111
00001111 | 10110101 = 10111111
Logical XOR, ^ with 00001111
00001111 ^ 10110101 = 10111010
Last edited on
@b1gzz base 2 is binary. http://www.purplemath.com/modules/numbbase.htm

I don't know what you mean by "how" they are used. It looks like you got the basic syntax, so I'm guessing you are not asking how to do it but wondering how you would use a value after the bit shift operation? An example I recently used is a robot simulator I was working on and I used bit shifting to store two 16 bit integers into one 32 bit integer like this:
data = xCoor << 16 | yCoor << 0;
This way the assembly coded robot doesn't have to make two requests for a value to determine it's x and y coordinates. It just has to do some bit masking to get the values which is less expensive.
Sheesh I'm confused too now...

Back in beginning electronics class that same issue messed with me for a while.

Simply this:
You get bytes right 11111111 is one byte of 8 bits.

AND:
If both are 1 then the result is 1 weather you do anything with the result or not.
1 AND 1 = TRUE
1 AND 0 = FALSE

AND= both are 1 yields true, else false.
OR = one or the other or both is 1 yields true.
XOR = one is 1 and the other is 0, yields true. If both are 1 or both are 0 yields false
NOT = just inverts a single value.

So each bit is dealt with the other by its same position.

00011010 XOR
00011110
----------------
00000100

Usually bit operators are used to test and change flag values. (bits in a variable)
OR would test if bits were set and yield a value showing all the set bits from the two.
XOR tests for differences.
AND tests bits set in both.

Don't confuse or as A || B with A | B.
The first says A or B is true and returns true.
The second says OR A to B and return the value.
C code would accept "A | B" automatically as true or false when compared to a Boolean variable.
C++ is leaning away from this as A OR B actually returns what ever A and B types are.

Meaning, depending on your compiler and the version of C++ from its beginning to future standards:
int A=2; int B=3;
bool result=0;
result = A | B;
Might or might not compile. The compiler says A|B returns an int not a bool.
Even though it is a positive value, it is a compiler cast from int to bool.



So base 2 is just 1*2=2, 2*2=4, 4*2=8, 8*2=16 and so on, being the 0's and 1's of binary. That's pretty easy to remember.

Yes, that's it @pnoid, I want to know how to use the values because I have no idea how they can be used like they were just "normal decimal numbers".
With your example I got a better idea on how to use them. But to do that data = xCoor << 16 | yCoor << 0; the values most be in bits (binary), right? And by doing that you're compressing the two values in a single integer, right?
By the way, shifting with 0 means it won't move?

robot doesn't have to make two requests for a value to determine it's x and y coordinates.
Do you mean or (|)? By using | (or) the robot would read that single integer (data) as 2 different "paths", am I right?

Well, if it's that then thank you for explaining me, it was much easier to understand :)
Thank you for the replies, people =D

Thanks as well, @vaportrack.
Last edited on
But to do that data = xCoor << 16 | yCoor << 0; the values most be in bits (binary), right? And by doing that you're compressing the two values in a single integer, right?
By the way, shifting with 0 means it won't move?

The values don't need to be in binary, they can be, but they don't need to be. In the program I used this code in the x and y coordinates were in regular base 10. Like if x = 350 and y = 400, then data = 22938000.

In hex data = 015E0190 (in case you're wondering, hex is base 16, it's easier to work with binary in base 16 than base 10). hex 15E is 350 in base 10 and hex 190 is 400 in base 10. Each hex digit is 4 binary bits so you can see that after the bit shift the higher 16 bits in data contain 350 and the lower 16 contain 400.

You can accomplish this by setting data to 350 x 2^16 + 400 and it's the same result but it's much more efficient to do it with bit shifting.
Last edited on
I already know about binary conversions to decimal, I already understand bitwise operations, I already understand boolean variables but I still don't understand how to use them :c

Let's say I've this code:
1
2
3
4
5
int main(){
	int value = 4;
	int test = value << 2;
	cout << test; //output would be 16
}

Just by using the bit shifting << 2 instead of *4 it makes the program faster and more efficient?

What about the bitwise operators? I know what they do but let's say I've this:
1
2
3
4
5
int main(){
	int value = 4;
	int test = value & 2;
	cout << test; //output would be 0
}

What are the advantages of using &, | and ^ if the output would be a number I didn't want? I would need to use the operators wisely, test them all first to see if I'd get the wanted value and only then I would be able to use them, am I right? Is that all or am I thinking in the wrong way?

Thanks.
I've only ever used them when I was working with assembly in some way, like the example I gave earlier where I was interfacing a c++ coded environment with assembly coded robots. It's not a huge performance boost on a modern computer but was a huge boost for a program ran on a simulated microcontroller. I'm actually kind of new to bitwise operations in c++. I completely forgot c++ even had bitwise operations until I ran into that bit masking issue and someone pointed out that I could use bit shifting instead of calculating the number mathematically, so even if the performance boost is negligible it saved me a few minutes of time not having to calculate values before I coded them.

I think bitwise operations are used a lot in encryption too.

Other than that, I don't really know. I don't use them much in higher level programming.
Jumping into this thread late.

I wrote up a huge explanation of bitwise operators as well as examples of practical uses here:
http://www.cplusplus.com/forum/beginner/72705/#msg387899

What are the advantages of using &, | and ^ if the output would be a number I didn't want?


Well if the result is not what you want, then you wouldn't use them =P

| and & are typically used to combine/isolate bitflags respectively. I explain more in the above link.

^ is typically used to toggle specific bits -- which can be useful for basic encryption.


Just by using the bit shifting << 2 instead of *4 it makes the program faster and more efficient?


No. If your goal is to multiply, then multiply. If your goal is to shift, then shift. Don't substitute one for the other. It usually won't make any performance difference and will just make your code more confusing and harder to follow.
Last edited on
Another practical use for & is to check if a number is even or odd.

std::cout << (number & 1 ? "odd" : "even") << std::endl;
Thanks for the help.
Topic archived. No new replies allowed.