The bitwise logical operators work on the data bit by bit, starting from the least significant bit, i.e. LSB bit which is the rightmost bit, working towards the MSB (Most Significant Bit) which is the leftmost bit. The result of the computation of bitwise logical operators is shown in the table given below. In this tutorial, you will learn-

What are Bitwise Operators? Bitwise AND
Bitwise OR
Bitwise Exclusive OR
Bitwise shift operators
Bitwise complement operator

Bitwise AND

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. The result of the bitwise AND operation is 1 if both the bits have the value as 1; otherwise, the result is always 0. Let us consider that we have 2 variables op1 and op2 with values as follows: The result of the AND operation on variables op1 and op2 will be As we can see, two variables are compared bit by bit. Whenever the value of a bit in both the variables is 1, then the result will be 1 or else 0.

Bitwise OR

It is represented by a single vertical bar sign (|). Two integer expressions are written on each side of the (|) operator. The result of the bitwise OR operation is 1 if at least one of the expression has the value as 1; otherwise, the result is always 0. Let us consider that we have 2 variables op1 and op2 with values as follows: The result of the OR operation on variables op1 and op2 will be As we can see, two variables are compared bit by bit. Whenever the value of a bit in one of the variables is 1, then the result will be 1 or else 0.

Bitwise Exclusive OR

It is represented by a symbol (^). Two integer expressions are written on each side of the (^) operator. The result of the bitwise Exclusive-OR operation is 1 if only one of the expression has the value as 1; otherwise, the result is always 0. Let us consider that we have 2 variables op1 and op2 with values as follows: The result of the XOR operation on variables op1 and op2 will be As we can see, two variables are compared bit by bit. Whenever only one variable holds the value 1 then the result is 0 else 0 will be the result. Let us write a simple program that demonstrates bitwise logical operators. Output:

Bitwise shift operators

The bitwise shift operators are used to move/shift the bit patterns either to the left or right side. Left and right are two shift operators provided by ‘C’ which are represented as follows: Here,

an operand is an integer expression on which we have to perform the shift operation. ‘n’ is the total number of bit positions that we have to shift in the integer expression.

The left shift operation will shift the ‘n’ number of bits to the left side. The leftmost bits in the expression will be popped out, and n bits with the value 0 will be filled on the right side. The right shift operation will shift the ‘n’ number of bits to the right side. The rightmost ‘n’ bits in the expression will be popped out, and the value 0 will be filled on the left side. Example: x is an integer expression with data 1111. After performing shift operation the result will be: Shifts operators can be combined then it can be used to extract the data from the integer expression. Let us write a program to demonstrate the use of bitwise shift operators. Output: After performing the left shift operation the value will become 80 whose binary equivalent is 101000. After performing the right shift operation, the value will become 5 whose binary equivalent is 000101.

Bitwise complement operator

The bitwise complement is also called as one’s complement operator since it always takes only one value or an operand. It is a unary operator. When we perform complement on any bits, all the 1’s become 0’s and vice versa. If we have an integer expression that contains 0000 1111 then after performing bitwise complement operation the value will become 1111 0000. Bitwise complement operator is denoted by symbol tilde (~). Let us write a program that demonstrates the implementation of bitwise complement operator. Output: Here is another program, with an example of all the operatoes discussed so far: After we compile and run the program, it produces the following result:

Summary

Bitwise operators are special operator set provided by ‘C.’ They are used in bit level programming. These operators are used to manipulate bits of an integer expression. Logical, shift and complement are three types of bitwise operators. Bitwise complement operator is used to reverse the bits of an expression.