Bit-wise operators are used to perform operations on the binary value of the number. These operators can be applied only on the Integral data types i.e., short, int, long, and also on char, as it can be converted to corresponding ASCII.
These operators are used for bit-manipulation in C. Bit manipulation is done on the numbers, whether they are signed or not. There are six bit-wise operators in C. The following table gives you description of the operators.
2 - 010
3 - 011
Then performs the AND operations, i.e., results in 1 if both are 1 and in 0 even if one of them is zero.
So in the units place, the bits are 0 and 1, so the result is 0. In ten's place, the bits are 1 and 1, so the result is 1. Thus we perform the AND operation for all the values and result is given out to be 010 i.e., 2.
2 - 010
3 - 011.
Performing OR operation takes 1 even if one of the bits is 1, else it returns 0. So in the above example, as units place is 0 and 1, it takes 1. In the tens place, it is 1 and 1, So it takes 1 again. Moving to the next, 0 and 0, So the result is 0. Thus the result is 011. Hence it is 3.
2 - 010
3 - 011
As per the truth table of Ex-OR, we take 0 if both the bits are same else the result is 1 i.e., if there is a combination of 0 and 1, then the result is 1 else, it is 0.
In the above example,
Only the LSB position is having a combination of 0 and 1, all other bits are the combinations of either 0 and 0 or 1 and 1. So the final result is 001 i.e., 1
These operators are used for bit-manipulation in C. Bit manipulation is done on the numbers, whether they are signed or not. There are six bit-wise operators in C. The following table gives you description of the operators.
Operator
|
Symbol
|
Example
|
Description
|
Bitwise AND
|
&
|
a&b
|
Performs bitwise AND operation and gives result.
|
Bitwise Inclusive OR
|
|
|
a|b
|
Performs bitwise OR operation and gives result.
|
Bitwise Exclusive OR
|
^
|
a^b
|
Performs Ex-OR operation and gives result.
|
One’s complementary
|
~
|
~a
|
Performs one’s complement and gives value.
|
Shift Left
|
<<
|
a<<1
|
Performs left shift of bits by 1
|
Shift Right
|
>>
|
a>>1
|
Performs right shift of bits by 1
|
All these operators are used to perform bitwise operations in C. Let us know about each of the operator in brief.
Bitwise AND:
This Operator takes two values and considers its binary value and performs the AND operation on both the values.
For example,
x=2&3;This results in the value 2 for x. This is because, when we perform the operation of 2&3, it first considers the binary representation of 2 and 3.
2 - 010
3 - 011
Then performs the AND operations, i.e., results in 1 if both are 1 and in 0 even if one of them is zero.
So in the units place, the bits are 0 and 1, so the result is 0. In ten's place, the bits are 1 and 1, so the result is 1. Thus we perform the AND operation for all the values and result is given out to be 010 i.e., 2.
Bitwise Inclusive OR:
This Operator also takes the binary representation of two numbers and performs the OR operation on bits and gives the result.
For example,
x=2|3;This results 3 for x. This is because,
2 - 010
3 - 011.
Performing OR operation takes 1 even if one of the bits is 1, else it returns 0. So in the above example, as units place is 0 and 1, it takes 1. In the tens place, it is 1 and 1, So it takes 1 again. Moving to the next, 0 and 0, So the result is 0. Thus the result is 011. Hence it is 3.
Bitwise Exclusive OR:
This operators performs the Ex-OR operation on the two numbers.
For example,
x=2^3;The result is 1 because of the Ex-OR.
2 - 010
3 - 011
As per the truth table of Ex-OR, we take 0 if both the bits are same else the result is 1 i.e., if there is a combination of 0 and 1, then the result is 1 else, it is 0.
In the above example,
Only the LSB position is having a combination of 0 and 1, all other bits are the combinations of either 0 and 0 or 1 and 1. So the final result is 001 i.e., 1