Friday, 30 August 2013

Bitwise operator | C Programming Language Tutorial pdf



Bitwise operator : 

Bitwise operator are used to perform operations at binary level i. e. bitwise. these operators are used for testing the bits, or Shifting them right or left . These operators are not applicable to float or double. Following are the Bitwise operators with their meanings.

    Operator       Meaning
        &           Bitwise AND
        |             Bitwise OR
        ^            Bitwise Exclusive – OR
       <<           Left Shift
       >>           Right Shift
       ~             Complement

Eg:  consider a = 13 & b = 6 as 8 bit short int (1byte)

<< Left Shift
a = 13    Binary    00001101
b = 6                   00000110

Consider a << 2 which Shifts two bits to left , that is 2 zeros are inserted at the right and two bits at the left are moved out.
       00001101
Moved
       00110100

Finally the result is 00110100 . Deci 52 (13x4)
Note : when you shift a bit towards left its Decimal Value is multiplied by Two (2).
a =13     13= 00001101
          a >>2 00000011
          000000 11 Decimal 3 (13/4)

Program:  WAP to illustrate the use of size of operator
main ( )
{
int x = 2;
float y = 2;
printf (“ in size of ( x ) is %d bytes “, sizeof ( x ));
printf (“ in size of ( y ) is %d bytes “, sizeof ( y ));
printf (“ in Address of x = % u and y = % u “, & x, & y);
}

Output:
sizeof ( x ) = 2
sizeof ( y ) = 4
Address of x = 4066 and y = 25096
~ Complement
convert 0?s & to 1?s and 1?s to O?s .
& (Bitwise logical and )      op1 op2 & |
a = 13 0000 1101               0     0    0 0
b = 6 0000 0110                0      1    0 1
a & b 0000 0100                1     0    0 1
                                      1 1    1 1

| (Bitwise Logical or )
a = 13 0000 1101
b = 6  0000 0110
a  1b  0000 1111

^ ( Bitwise Exclusive or ) op1 op2 ^
0 0 0
a = 13 0000 1101 0 1 0
b = 6 0000 0110 1 0 1
a ^ b 0000 1011 1 1 0

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved