**Free4Dev **– In digital computer programming, a **bit operator** operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast and simple action, directly supported by the processor, and is used to manipulate values for comparisons and calculations – from Wikipedia.

`|`

– bitwise OR

See this example to understand OR bitwise:

0 1 2 3 4 5 6 |
int a = 5; // 0101b (0x05) int b = 12; // 1100b (0x0C) int c = a | b; // 1101b (0x0D) std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl; |

**Ouput**

0 1 2 |
a = 5, b = 12, c = 13 |

**Why?**

A bit wise OR operates on the bit level and uses the following Boolean truth table:

0 1 2 3 4 |
true OR true = true true OR false = true false OR false = false |

When the binary value for `a`

(`0101`

) and the binary value for `b`

(`1100`

) are OR’ed together we get the binary value of `1101`

:

0 1 2 3 4 5 |
int a = 0 1 0 1 int b = 1 1 0 0 | --------- int c = 1 1 0 1 |

The bit wise OR does not change the value of the original values unless speciﬁcally assigned to using the bit wise assignment compound operator `|=`

:

0 1 2 3 |
int a = 5; // 0101b (0x05) a |= 12; // a = 0101b | 1101b |

`^`

– bitwise XOR (exclusive OR)

0 1 2 3 4 5 6 |
int a = 5; // 0101b (0x05) int b = 9; // 1001b (0x09) int c = a ^ b; // 1100b (0x0C) std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl; |

**Output**

0 1 2 |
a = 5, b = 9, c = 12 |

**Why?**

A bit wise XOR (exclusive or) operates on the bit level and uses the following Boolean truth table:

0 1 2 3 4 5 |
true XOR true = false true XOR false = true false XOR false = false false XOR true = true |

Notice that with an XOR operation `true XOR true = false`

where as with operations `true AND/OR true = true`

, hence the exclusive nature of the XOR operation.

Using this, when the binary value for `a`

(`0101`

) and the binary value for `b`

(`1001`

) are XOR’ed together we get the binary value of `1100`

:

0 1 2 3 4 5 |
int a = 0 1 0 1 int b = 1 0 0 1 ^ -------- int c = 1 1 0 0 |

The bit wise XOR does not change the value of the original values unless speciﬁcally assigned to using the bit wise assignment compound operator `^=`

:

0 1 2 3 |
int a = 5; // 0101b (0x05) a ^= 9; // a = 0101b ^ 1001b |

The bit wise XOR can be utilized in many ways and is often utilized in bit mask operations for encryption and compression.

**Note**: The following example is often shown as an example of a nice trick. But should not be used in production code (there are better ways `std::swap()`

to achieve the same result).

You can also utilize an XOR operation to swap two variables without a temporary:

0 1 2 3 4 5 6 7 8 9 10 |
int a = 42; int b = 64; // XOR swap a ^= b; b ^= a; a ^= b; std::cout << "a = " << a << ", b = " << b << "\n"; |

To productionalize this you need to add a check to make sure it can be used.

0 1 2 3 4 5 6 7 8 9 10 11 |
void doXORSwap(int & a, int & b) { // Need to add a check to make sure you are not swapping the same // variable with itself. Otherwise it will zero the value. if ( & a != & b) { // XOR swap a ^= b; b ^= a; a ^= b; } } |

So though it looks like a nice trick in isolation it is not useful in real code. xor is not a base logical operation,but a combination of others: `a^c=~(a&c)&(a|c)`

also in 2015+ compilers variables may be assigned as binary:

0 1 2 |
int cn = 0b0111; |

`&`

– bitwise AND

0 1 2 3 4 5 6 |
int a = 6; // 0110b (0x06) int b = 10; // 1010b (0x0A) int c = a & b; // 0010b (0x02) std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl; |

**Output**

0 1 2 |
a = 6, b = 10, c = 2 |

**Why?**

A bit wise AND operates on the bit level and uses the following Boolean truth table:

0 1 2 3 4 |
true AND true = true true AND false = false false AND false = false |

When the binary value for `a`

(`0110`

) and the binary value for `b`

(`1010`

) are AND’ed together we get the binary value of `0010`

:

0 1 2 3 4 5 |
int a = 0 1 1 0 int b = 1 0 1 0 & -------- int c = 0 0 1 0 |

The bit wise AND does not change the value of the original values unless speciﬁcally assigned to using the bit wise assignment compound operator `&=`

:

0 1 2 3 |
int a = 5; // 0101b (0x05) a &= 10; // a = 0101b & 1010b |

`<<`

– left shift

0 1 2 3 4 5 |
int a = 1; // 0001b int b = a << 1; // 0010b std::cout << "a = " << a << ", b = " << b << std::endl; |

**Output**

0 1 2 |
a = 1, b = 2 |

**Why?**

The left bit wise shift will shift the bits of the left hand value (`a`

) the number speciﬁed on the right (1), essentially padding the least signiﬁcant bits with 0’s, so shifting the value of `5`

(binary `0000 0101`

) to the left 4 times (e.g. `5 << 4`

) will yield the value of `80`

(binary `0101 0000`

). You might note that shifting a value to the left 1 time is also the same as multiplying the value by 2, example:

0 1 2 3 4 5 6 7 8 9 10 11 |
int a = 7; while (a < 200) { std::cout << "a = " << a << std::endl; a <<= 1; } a = 7; while (a < 200) { std::cout << "a = " << a << std::endl; a *= 2; } |

But it should be noted that the left shift operation will shift *all* bits to the left, including the sign bit, example:

0 1 2 3 4 5 |
int a = 2147483647; // 0111 1111 1111 1111 1111 1111 1111 1111 int b = a << 1; // 1111 1111 1111 1111 1111 1111 1111 1110 std::cout << "a = " << a << ", b = " << b << std::endl; |

Possible output: `a = 2147483647`

, `b = -2`

While some compilers will yield results that seem expected, it should be noted that if you left shift a signed number so that the sign bit is aﬀected, the result is **undeﬁned**. It is also **undeﬁned** if the number of bits you wish to shift by is a negative number or is larger than the number of bits the type on the left can hold, example:

0 1 2 3 4 |
int a = 1; int b = a << -1; // undefined behavior char c = a << 20; // undefined behavior |

The bit wise left shift does not change the value of the original values unless speciﬁcally assigned to using the bit wise assignment compound operator `<<=`

:

0 1 2 3 |
int a = 5; // 0101b a <<= 1; // a = a << 1; |

`>>`

– right shift

0 1 2 3 4 5 |
int a = 2; // 0010b int b = a >> 1; // 0001b std::cout << "a = " << a << ", b = " << b << std::endl; |

**Output**

0 1 2 |
a = 2, b = 1 |

**Why?**

The right bit wise shift will shift the bits of the left hand value (a) the number speciﬁed on the right (1); it should be noted that while the operation of a right shift is standard, what happens to the bits of a right shift on a signed negative number is *implementation deﬁned* and thus cannot be guaranteed to be portable, example:

0 1 2 3 |
int a = -2; int b = a >> 1; // the value of b will be depend on the compiler |

It is also undeﬁned if the number of bits you wish to shift by is a negative number, example:

0 1 2 3 |
int a = 1; int b = a >> -1; // undefined behavior |

The bit wise right shift does not change the value of the original values unless speciﬁcally assigned to using the bit wise assignment compound operator `>>=`

:

0 1 2 3 |
int a = 2; // 0010b a >>= 1; // a = a >> 1; |

**You may also like:**