Thursday, February 4, 2010

Logical Operator and Bitwise Operator

#include< stdio.h >
main()
{
int i=4,j=8;
printf("%d %d %d",i|j,i|j&&j|i,i&j);
}

output:


12      1      0


How?


if you familiar with bitwise operator and Logical operators,you can easily understand the logic of this program.

Logical Operators and it's operations

|| (OR Operator)- this will check any one expression is true. if so,it'l
return true value (1) otherwise false(0) will be return.
eg:
a=1 > 2||2 < 3;

here 'a' value will be 1 because 2 < 3 expression is true.
&& (And Operator)- This will check both expression is true. if so ,it'll
return true value otherwise false will be return.
Eg:
a=1 > 2&&2 < 3;
here 'a' value will be 0 because 1 > 2 is false though 2 < 3 is true so it'll return
false(0).
Biwise operator

| (bitwise OR operator)- This will do OR operation to given numbers and give a resultant
number.
Eg:
a=1 | 3 ;
here a=3 because this will OR the 1 and 3 like this

1: 0001
3: 0011
_____
result: 0011 -- > 3
& (Bitwise AND operator)- This will do AND operator to given number and give a
resultant number.
Eg:
a=1 & 3;
here a=1 .
The And Operation:
1: 0001
3: 0011
________
result: 0001 - > 1

^ (Bitwise X-OR operator)-Normal ex-or operation.
Eg:
a= 1 ^ 3;
here a=2.
The Ex-or operation:
1: 0001
3: 0011
________
result: 0010 -- > 2.
> > (Right shift operaot). shifting bits to right side(simply LSB will be removed
and MSB will be 0 .
eg:
a=2 > > 1;
here '2' bits are shifted 1 bit right side.

2: 0010 > > 1
the result 0001.
so a=1.

< < ( Left Shift Operator). Shifting bits to left side ( MSB will be removed and LSB
will be 0.
Eg:
a=2 < < 1;
here '2' bits are shifted 1 bit left side.
2: 0010 < < 1
the result 0100.
so a=4.

0 comments:

Post a Comment