# Logical Operations on Bit Patterns

• 02-16-2008
mthemapc
Logical Operations on Bit Patterns
I'm reading a book I got from the library to learn C++ and I don't understand this.
Quote:

~ This is the bitwise complement operator. This is a unary operator that will invert the bits in its operand, so 1 becomes 0 and 0 becomes 1.

& This is the bitwise AND operator, which will AND the corresponding bits in its ioerands. If the corresponding bits are both 1, then the result bit is 1. Otherwise it is 0.

^ This is the bitwise exclusive OR operator, which will exclusive-OR the corresponding bits in its operands. If the corresponding bits are different, i.e. one is 1 and the other is 0, then the resulting bit is 1. If the corresponding bits are the same, the resulting bit is 0.

| This is the bitwise OR operator, which will OR the corresponding bits in its operands. If either of the two corresponding bits is 1, then the result is 1. If both bits are 0, then the result is 0.
That is the exact quote from the book and I don't understand when you would use them and why you would need too and really what they do. Someone please explain it for a beginner.
• 02-16-2008
Herz
Basically, all of them, except ~, compare 2 bits and return a value, either 0 or 1.

~ Just inverts a bit. So ~1 = 0 and ~0 = 1. Think of it as "not true = false" and "not false = true"

There are many uses for bitwise operators (your computer wouldnt work without them) such as bit masking or calculating Two's complement

Read the wikipedia article on bitwise operators for a more detailed explanation.
• 02-16-2008
cyberfish
do you not understand what they do or how they can be used?
• 02-17-2008
mthemapc
Herz explained what they do but I don't understand how they can be used still
• 02-17-2008
vart
You will use them when you need to extract or set individual bits of the byte

For example if you want to check that first 3 bits of the byte are 0
You will use & with mask where these bits are set to 1 and others to 0 to "remove" bits that you are not interested in
And when compare result to 0
Code:

```if (x & 0x7 == 0) {   // 3 left bits are zeroes }```
This operations are very usefull in networking for example, because most network protocols operate with bit-streams (not byte streams)

Or some media stream description - take H264 format and see it by yourself
• 02-17-2008
tabstop
For an example, look up all the flags in std::ios_base::flags. I blatantly stole this example from cplusplus.com:
Code:

```// modify flags #include <iostream> using namespace std; int main () {   cout.flags ( ios::right | ios::hex | ios::showbase );   cout.width (10);   cout << 100;   return 0; }```
Each of the flags stands for integer with one binary digit turned "on" (or at least we can believe so, even though the details are hidden inside the implementation). If we | those numbers together, we get a new integer with a 1 wherever there was a 1 in any of the original things, so that integer basically carries all three of our flags.

If we want to know whether a flag was set, we can use & with that flag (if both are 1 in the same spot, we'll get a 1 back; if either the flag or the status has a 0 somewhere, we'll get a 0).
• 02-17-2008
cyberfish
as for why not use booleans instead, it takes 1/8 the space (compression). A bool is 1 bytes (8 bits), and only one of them is used, whereas you can use all eight bits using bit flags.
• 02-17-2008
anon
Also consider passing things to functions. Would you rather use a function that required you to pass 32 separate bools or 1 unsigned?