Let us learn The Bitwise Operators:
Bitwise operations are fast and simple,basic to higher-level arithmetic operations and directly supported by processor.
Bitwise operations are faster than normal addition,subtraction,multiplication and division.
Lets learn bitwise Operators one by one.
1. & Bitwise AND Operator
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.
Truth table for bitwise operators
Bitwise Operators in C Language
In this article, we are discussing the Bitwise operators and their types. Here, we will discuss all types of Bitwise operators used in C programming language.
.
Bitwise operators in c programming are the special type of operators are generally used to perform operation on the at data bit level.When the operations perfomed on bit level then it is called bit level programming.
In the arithmetic-logic unit (which is within the CPU), mathematical operations like: addition, subtraction, multiplication and division are done in bit-level.
C language use following types of Bitwise operators:
1. & Bitwise AND Operator.
2. | Bitwise OR Operator.
3. ^ Bitwise Exclusive OR Operator.
4. << Bitwise Left shift Operator.
5. >> Bitwise Right Shift Operator.
6. ~ One's Compliment Operator.
a b a&b a|b a^b
----------------------------
0 0 0   0    0
----------------------------
0 1 0   1    1
----------------------------
1 0 0   1    1
----------------------------
1 1 1   1    0
for example,
We have two variables a and b.
a =5;
b=3;
The binary representation of the above two variables values 5 and 3 are given below:
a = 0101
b = 0011
When we apply the bitwise AND operation in the above two variables, i.e. a&b then the output would be:
Result = 0001
As we can observe from the above result that bits of both the variables are compared one by one. If the bit of both the variables is 1 then the output would be 1, otherwise 0.
C Program to show working of Bitwise AND & Operator
#include <stdio.h>
int main()
{
int a=5, b=3,c; /* integer variable declarations */
c=a&b;
printf("The output of the Bitwise AND operator a&b is %d",c);
return 0;
}
Output:
The output of the Bitwise AND operator a&b is 1
In the above code, we have created and initilized three integer variables, i.e., a=5, b=3 and c=0 respectively.
The binary value of 'a' and 'b' are 0101 and 0011, respectively. When we apply the AND operator between these two variables,
a & b = 0101 && 0011 = 0001
Bitwise Operator Example
a | b | a&b | a|b | a<<b | a>>b |
---|---|---|---|---|---|
5 | 3 | 1 | 7 | 40 | 0 |
2. | Bitwise OR Operator
The bitwise OR operator is represented by a single vertical sign (|). Two integer operands are written on both sides of the (|) symbol. If the bit value of any of the operand is 1, then the output would be 1, otherwise 0.
The result upon applying bitwise OR | becomes 1 if any one of of the operands bit value is 1 . Otherwise, it returns 0 as the value.
This type of operator returns 1 even when both or even one of the operand bit is 1 otherwise return 0. For instance, the p | q will return 1 when both or one of p and q bit value is 1.
Syntax: (var1 | var2)
e.g. If c = 5 and d = 2 then, expression c|d equals to 7.
C Program to show working of Bitwise OR | Operator
#include <stdio.h>
int main()
{
int a=5, b=3,c=0; /* integer variable declarations */
c=a|b;
printf("The output of the Bitwise OR operator a|b is %d",c);
return 0;
}
Output:
The output of the Bitwise OR operator a|b is 7
In the above code, we have created and initilized three integer variables, i.e., a=5, b=3 and c=0 respectively.
The binary value of 'a' and 'b' are 0101 and 0011 respectively. When we apply the OR operator between these two variables,
a | b = 0101 && 0011 = 0111
3. Bitwise XOR Operator ^
Bitwise XOR (exclusive OR) ^ is an Operator in C Programming language that provides the answer 1 (true) if both of the bits in the operands are different .
If both of the bits are same then XOR (^) Operator gives the result 0(false).
The XOR Operator It is denoted by the Symbol ^.
Let us understand how XOR ^ Operator work by using Following example.
10 = 00001010 (In Binary)
12 = 00001100 (In Binary)
Bitwise XOR Operation of 10 and 12
00001010
^
00001100
________
00000110 = 6 (In decimal)
Example : Bitwise XOR
C Program to show working of Bitwise XOR ^ Operator
#include <stdio.h>
int main()
{
int a = 10, b = 12;
printf("Result after using xor ^ Operator= %d", (a ^ b));
return 0;
}
Output
Result after using xor ^ Operator= 6
4. Bitwise Left Shift Operator <<
Bitwise Left shift operator is the logical bitwise and binary operator in C Programming.It is called binary operator because this operator operates on two positive integral Operands.
It shifts all bits towards left by a certain number of specified bits. The bit positions that have been vacated by the left shift operator are filled with 0. The symbol of the left shift operator is <<.
Syntax: Operand << n
Where,
Operand is an integer expression on which we apply the left-shift operation.
n is the number of bits to be shifted.
Here n number of bits will be shifted to left side.
The 'n' bits on the left side will be popped out, and 'n' bits on the right-side are filled with 0.
For example,
Suppose we have a statement:
int a = 5;
The binary representation of 'a' is given below:
a = 0101
One thing we need to keep in mind that when we shift 1 bit Postion to the left then it becomes double and becomes half(neglects the fractional part) when shifted to right.
If we want to left-shift the above representation by 2 to left, then the statement would be:
a << 2;
0101<<2 = 00010100
Binary number 00010100 is 20 in decimal.
5. Bitwise Right Shift Operator >>
Bitwise right shift operator is the logical bitwise and binary operator in C Programming.It is called binary operator because this operator operates on two positive integral Operands.
It shifts all bits towards right by a certain number of specified bits. The symbol of the right shift operator is >>.
When ever we perform this operation on an integer a with integer b the result obtained after always equal to division of 2^b.
For example 1.
a=40
b=1
a=a>>b
thus the result obtained will be 20.
a/2^1 ---> 40/2--->20
For example 2
Suppose we have a statement:
int a = 5;
The binary representation of 'a' is given below:
a = 0101
One thing we need to keep in mind that when we shift 1 bit Postion to the right then it becomes half (neglects the fractional part)
If we want to shift the above representation by 1 to right, then the statement would be:
a >> 1;
0101>>1 = 00000010
Binary number 00000010 is 2 in decimal.
6. Compliment Operator ~
Bitwise Complement Operator ~ also called One's Compliment Operator ~ . It produce a bitwise one's complement of it's operand.
Bitwise complement operator is a unary operator (works on only one operand). It changes 1 to 0 and 0 to 1. It is denoted by ~.
35 = 00100011 (In Binary)
Bitwise complement Operation of 35
~ 00100011
________
11011100 = 220 (In decimal)
The bitwise complement of 35 (~35) is -36 instead of 220, but why?
For any integer n, bitwise complement of n will be -(n + 1). To understand this, you should have the knowledge of 2's complement.
2's Complement
Two's complement is an operation on binary numbers. The 2's complement of a number is equal to the complement of that number plus 1
The bitwise complement of 35 is 220 (in decimal). The 2's complement of 220 is -36. Hence, the output is -36 instead of 220.
Bitwise Complement of Any Number N is -(N+1).
Here's how:
bitwise complement of N = ~N (represented in 2's complement form)
2'complement of ~N= -(~(~N)+1) = -(N+1)
C Program to demonstrate Working of Bitwise Operator.
/*This C program that shows how Bitwise &,|,~,^,>>,<< operators works.< */br>
#include <stdio.h>
int main()
{
/* a = 5 binary of 5 is 00000101
b = 9 binary of 9 is 00001001 */
unsigned char a = 5, b = 9;
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a & b);
printf("a|b = %d\n", a | b);
printf("a^b = %d\n", a ^ b);
printf("~a = %d\n", a = ~a);
printf("b<<1 = %d\n", b << 1);
printf("b>>1 = %d\n", b >> 1);
return 0;
}
Output:
a = 5
b = 9
a&b = 1
a|b = 13
a^b = 12
~a = -6
b<<1 =18
b>>1 = 4
Program Explanation:
In our program we declared and initilise the variable a=5,b=9
we used print fuction to show values of a and b
the output is
a=5
b=9
next we got the output
a&b = 1:
In this expression value of variable a is 5 whose binary equivalent is 00000101 and the value of variable b is 9 and the binary equivalent is 00001001.
Binary & operator works on bits of each operand.Let find out how a&b=1 becomes.
a= 00000101
b= 00001001
a&b=00000001 whic is equal to 1 in decimal hence output a&b=1
Note:when ever bitwise & operation is performed on operand it gives output bit 1 when both operands bits are 1 other wise gives 0
a|b = 13:
Binary | operator works on bits of each operand.Let find out how a|b=13 becomes.
a= 00000101
b= 00001001
a|b=00001101 whic is equal to 13 in decimal hence output a|b=13
Note:when ever bitwise | operation is performed on operand it gives output bit 1 when one of the operands bits are 1 other wise gives 0
a^b = 12:
Binary ^ operator works on bits of each operand.Let find out how a^b=12 becomes.
a= 00000101
b= 00001001
a^b=00001100 whic is equal to 12 in decimal hence output a^b=12
Note:when ever bitwise ^ operation is performed on operand it gives output bit 1 when the operands bits are odd, and gives 0 when operands bits are same.
b<<1 =18:
Binary << left shift operator works on bits of each operand.Let find out how a<< 1 =18 becomes.
b= 00001001
b<<1 shift the one bit to the left, after shifting one bit to left the final value becomes b=00010010 which is 18 in decimal.
b>>1 = 4:
Binary >> left shift operator works on bits of each operand.Let find out how a<< 1 =18 becomes.
b= 00001001
b>>1 shift the one bit to the right, after shifting one bit to right the final value becomes b=00000100 which is 4 in decimal.
Previous Topic:-->> Logical Operators || Next topic:-->>? ,* ,&,sizeof() Operators