 # C++ Programming Code Examples

## C++ > Beginners Lab Assignments Code Examples

### Bitwise operators are similar to the logic operators, but they perform the same logical operations on bits.

``` Bitwise operators are similar to the logic operators, but they perform the same logical operations on bits. All data in memory is represented in the binary form. So, variables in form of bits contains only 0 or 1. The following table represents the result of operations for the bitwise operators: X Y X & Y X | Y X ^ Y 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 1 1 Binary AND operator "&" The resultant bit is set to 1 if and only if both variables have 1 in the corresponding bit. Example of binary &: 10100110 & 00101010 = 00100010 Binary OR operator "|" The resultant bit is set to 1 if at least one of the variables has 1 in the corresponding bit. Example of binary |: 10100110 | 00101010 = 10101110 Binary XOR operator "^" The result bit is set to 1 if only one of the variables has 1 in the corresponding bit. Example of Binary xor: 10100110 ^ 00101010 = 10001100 Binary NOT operator "~" Flips the bits of the variable. For Example: ~10100110 = 01011001 Binary Left Shift Operator "<< N" Will shift 'N' number of bits to the left. In simple words, N number of bits from the Left will be removed and N number of Zeros will be added to the Right. For Example: 10100110 << 3 = 00110000 Binary Right Shift Operator ">> N" Will shift 'N' number of bits to the right. In simple words, N number of bits from the Right will be removed and N number of Zeros will be added to the Left. For Example: 10100110 >> 2 = 00101001 ``` 