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
As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:
The bitwise operators are the operators used to perform the operations on the data at the bit-level. When we perform the bitwise operations, then it is also known as bit-level programming. It consists of two digits, either 0 or 1. It is mainly used in numerical computations to make the calculations faster. We have different types of bitwise operators in the C++ programming language. The following is the list of the bitwise operators: Bitwise AND operator is denoted by the single ampersand sign (&). Two integer operands are written on both sides of the (&) operator. If the corresponding bits of both the operands are 1, then the output of the bitwise AND operation is 1; otherwise, the output would be 0. This is one of the most commonly used logical bitwise operators. It is represented by a single ampersand sign (&). Two integer expressions are written on each side of the (&) operator.
This algorithm takes the input of a string with all distinct characters 'N' Value. It places each character to every index by Swapping Values. A function to 'swap character' values of string