Bitwise operator

This type of operator is used to perform operation at bit level.The bitwise operator are six types -

 symbols of opearotors 
 Name of operators
                &
 Bitwise AND
                 |
 Bitwise OR
                ~
 Bitwise NOT
               >>
 Right shift
               <<
 Left shift
                ^
 Bitwise XOR

Step to solve Bitwise operation -
These are three step to solve the bitwise operation -
step 1.)  convert all given number in binary numbers.
step 2.) perform the logical operation.
step 3.) convert the binary output into decimal number.

Bitwise AND (&) operator 


It is work same as logical  AND operators but at bit level  means on binary numbers. If both side of the binary numbers(of both operand )are true(1) value then  taken true(1)otherwise false value (0).
For program example -:
#include<stdio.h>
main()
{
int a=5, b=3,c;
c = a & b;
printf("%d",c);
}

Step 1.) convert both operands in binary numbers.
5 = 101;
3 = 011;
Step 2.) apply bitwise AND operaor on both side of binary number
                     5 =  101
                     3 =  011
Binary output   =  001
Step 3.)   convert the binary output in decimal number.
                 
         001 =>  1
Output :-        1

Bitwise OR (|) operator 


It is work same as logical  OR  operators but at bit level  means on binary numbers. If  any one from both side of the binary numbers ( of both operand )  are true(1) value then  taken true(1) and if  both side true(1) then also taken true(1) otherwise taken false(0) value.
For example :-

#include<stdio.h>
int main()
{
int a=5, b=3,c;
c = a | b;
printf("%d",c);
return 0;
}
Output : 7
Run this code

Let us solve by same three steps -
5 => 101
3 => 011
apply bitwise OR operator -
                5 =  101
                3 =  011
 Binary output   =  111

convert the binary output in decimal number.
111 =>  7
Output :     7

Bitwise XOR operator


Bitwise XOR operator works with two operands.In this case if  both values in  converted binary number is same then taken false(0) otherwise true(1).
For example :-

#include<stdio.h>
int main()
{
int a = 12,b = 10;
printf("%d",a^b);
return 0;
}
Output : 6
Run this code

step 1.) convert the both numbers in binary number.
12 => 1100
10 => 1010
step 2.) after applying the Bitwise XOR .
  a    =  12 => 1100
  b    =  10 => 1010
          a^b =>  0110
step 3.) convert the binary output in decimal number.

0110 => 6

Output :  6

Right  shift operator (>>)


Right shift operator  works only on the single operands.In this case first convert the given number in binary number before applying the Right shift operator.After applying the operator remove the number up to which we want to apply the Right shift operator on the converted binary number.
For example :-

#include<stdio.h>
int main()
{
int  a = 13, b;
b= a>>2;
printf("%d",b);
return 0;
}
Output : 3
Run this code

Step 1.)convert the given number in binary number.
 13 => 1101
Step 2.) remove the two numbers from right side of binary numbers.
1101 => 11
step 3.) convert the binary output to decimal number.
11 => 3
Output :   3

Left shift operator (<<) 


Left shift operator  works only on the single operands.In this case first convert the given number in binary number before  applying  the Left shift operator . After applying the operator add the number up to which we want to apply the Left shift operator on the converted binary number.
For example :-

#include<stdio.h>
int main()
{
int a = 5;
printf("%d",a<<2);
return 0;
}
Output : 20
Run this code

Step 1.)convert the given number in binary number.
 5 => 101
Step 2.) add the two numbers from right side of binary numbers.
101 => 10100
step 3.) convert the binary output to decimal number.
10100 => 20
Output :-  20

Assignment operator (=)


This operator is used only for assigning the value in a variable.It has the associativity left to right,Value becomes assign  left to right in a variable.
For example :-

#include<stdio.h>
int main()
{
int a;
a = 10;
printf("%d",a);
return 0;
}
Output : 10
Run this code

Comma operator (,)

In this operator value  assign from left to right. But  it has low precedence assignment operator.Basically it is used for making a group of expression.for example -

#include<stdio.h>
int main()
{
int a;
a = (1,2,3,4,5);
printf("%d",a);
return 0;
}
Output :  5
Run this code

Notes
keep in mind assignment operator has high precedence so we need to write  parenthesis .If we will not  write parenthesis before the numbers then  assign first value in the variable.
for example.

#include<stdio.h>
int main()
{
int a;
a=1,2,3,4,5;
printf("%d",a);
return 0;
}
Output :  1
Run this code

Sizeof operator

This operator is used to check size of data type. For example :-

#include<stdio.h>
int main()
{
int a=10;
printf("%d,",sizeof(a));
printf("%d",sizeof(10));
return 0;
}
Output :  4,4
Run this code

Notes -  Keep in mind size of integer data type is 4.And data type of 10 in the example also is a integer. So size of it is also  also 4 bytes.


<<Prev                                 Next>>