C program using bitwise right shift(>>)

EX: write a C program to perform bitwise right shift operation. How to write a C program to perform bitwise right shift operation of intiger. C program to perform bitwise right shift operation of intiger.


Input from user:


Enter the number:15

Enter how many numbers you wan't to right shift:2


Expected Output:

Output after performing right shift operation is: 3






  Logic to perform right shift operation of two intiger's:


 Firstly you need to know how right shift operator works.



  •  It  takes two numbers and right shift the bits of the first operand, the second  operand decides the number of places to shift.


    1. We take two integer numbers i.e, no=15 and s=2.

    Where no shows the number which you wan't to right shift and s shows the how many bits you want to right shift.

     2. Binary representation of no>>s  is :


          no    = 00001111
    ------------------------------------ 
     no>>s   = 00000011 =3(in decimal)

    3. After performing right shift operation 8 bit binary representation is 0000 0011 which is 3 in decimal.

    4. So output of the program is  3.




      C program using bitwise right shift operator :


    #include<stdio.h>
    void main()
    {
     int no,s;

     printf("Enter the number\n");
     scanf("%d",&no);

     printf("Enter how many bits you wan't to right shift\n");
     scanf("%d",&s);

     printf("Output after performing right-shift operation is:%d",no>>s);
    }


    Above program shows the following output:

    C program using bitwise right shift operator, Example program using bitwise right shift operator

    C program using bitwise left shift(<<)

    EX: write a C program to perform bitwise left shift operation. How to write a C program to perform bitwise left shift operation of intiger. C program to perform bitwise left shift operation of intiger.


    Input from user:


    Enter the number:15

    Enter how many numbers you wan't to left shift:1

    Expected Output:

    Output after performing left shift operation is: 30





      Logic to perform left shift operation of two intiger's:


     First you need to know how left shift operator works.




    •  It  takes two numbers and left shift the bits of the first operand, the second  operand decides the number of places to shift.


    1. We take two intiger numbers i.e, no=15 and s=1.

    Where no shows the number which you wan't to left shift and s shows the how many bits you want to left shift.

     2. Binary representation of no<<s is-


          no    = 00001111
    ------------------------------------ 
     no<<s   = 000011110 =30(in decimal)

    3. After performing left shift operation 8 bit binary representation is 00001 1110 which is 30 in decimal.

    4. So output of the program is  30.



      C program using bitwise left shift :


    #include<stdio.h>
    void main()
    {
    int no,s;
    printf("Enter the number\n");
    scanf("%d",&no);
    printf("Enter how many bits you wan't to left shift\n");
    scanf("%d",&s);
    printf("Output after performing left-shift operation is:%d",no<<s);

    }


    Above program shows the following output:

    C program using bitwise left shift(<<), C program using bitwise left shift(<<)

    C program using bitwise compliment (~)

    EX: write a C program to perform Compliment operation of two intiger's. How to write a C program to perform Compliment operation of two intiger's. C program to perform Compliment operation of two intiger's.

    Input from user:

    a=25;

    Expected Output:

    After performing Compliment operation the output is: 230






      Logic to perform Compliment operation of two intiger's:


     First you need to know how Compliment operator works.


    • It sets bit value 1 only if corresponding bit of operand is 0. Means it changes 1 value to 0 and 0 value to 1.

    1. We take one intiger numbers a=25.

     2. Binary representation of a -Compliment is-


          a      = 0001 1001
    ------------------------------------
          a~   = 1110 0110 =230 in decimal.

    3. After performing
    Compliment (~), 8 bit binary representation is 1110 0110 which is 230 in decimal.

    But output is not 230. The output of the Program is -26.
    To understand this you need to know how work's 2's compliment.



    •   2's compliment:

    For any intiger n, bitwise Complement of n is -(n+1).

    Means  2's compliment of a number is equal to the compliment of that number plus 1.


    For Example: 

    We take 230. 8 bit binary representation of 230 is 1110 0110 so 2's compliment of that number is...                                             -(0001 1001)

    -0001 1001
    +                1
    --------------------
    -0001 1010=-26(in decimal)



    The bitwise Complement of 25 is 230 in decimal. The 2's compliment of 230 is -26. Hence, the output is -26 not  230.





      C program using bitwise compliment:


    #include<stdio.h>
    void main()
    {
    int a=25;

    printf("Output is:%d",~a);

    }


    Above program shows the following output:

    C program using bitwise compliment, write a C program using bitwise compliment (~)