Bitwise Operators in C Language

Operators Definition

As studied earlier, Operators are the symbols, which performs operations on various data items known as operands. For Example: in a a+b, a and b are operands and + is an operator.

Note:- To perform an operation, operators and operands are combined together forming an expression. For example, to perform an addition operation on operands a and b, the addition(+) operator is combined with the operands a and b forming an expression.


Bitwise Operators

Bitwise operators are used to perform bit-by-bit operations. This operator can be applied to all the primitive data types such as long, int, short, char and byte etc.

Various bitwise operators are bitwise AND(&), bitwise OR(!), bitwise exclusive OR(^) one's complement(~), shift left (<<), shift right(>>), shift right with zero fill (>>>).

The table given below lists the various bitwise operators.

Operator Description Example
& Bitwise AND Operator copies a bit to the result if it exists in both operands. (A & B) = 12, i.e., 0000 1100
| Bitwise OR Operator copies a bit if it exists in either operand. (A | B) = 61, i.e., 0011 1101
^ Bitwise XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) = 49, i.e., 0011 0001
~ Bitwise One's Complement Operator is unary and has the effect of 'flipping' bits. (~A ) = ~(60), i.e,. -0111101
<< Bitwise Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 = 240 i.e., 1111 0000
>> Bitwise Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 = 15 i.e., 0000 1111

Example:



Output:

 Line 1 - Value of c is 12
 Line 2 - Value of c is 61
 Line 3 - Value of c is 49
 Line 4 - Value of c is -61
 Line 5 - Value of c is 240
 Line 6 - Value of c is 15								


Read Also