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 (~)




    C program using bitwise XOR(^)

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

    Input from user:

    a=13 , b=24;

    Expected Output:

    After performing XOR operation the output is: 21






      Logic to perform XOR operation of two intiger's:


     Firstly you need to know how work XOR operator.


     Note: The result of XOR is 1 if the two bits are different.

    1. We take two intiger numbers a=13 and b=24.

     2. Binary representation of a XOR b is-


          a      = 0000 1101

          b      = 0001 1000
    ------------------------------------
          a|b   = 0001 0101 =21

    3. After performing XOR operation 8 bit binary representation is 0001 0101 which is 21 in decimal.


    4. So output of the program is  21.




      C program using bitwise XOR :


    #include<stdio.h>
    void main()
    {
    int a=13,b=24;

    printf("After performing XOR operation the output is:%d",a^b);

    }


    Above program shows the following output:


    C program using bitwise XOR operator, C program using bitwise XOR

    C Program to Create a File and Write Data in it using fputs() and fputc() Functions

    In this tutorial we will learn how to create a file and write data in it....

    EX: Write a C program to create a file and write data in it, How to write to a file in C. C program to write data into a file.


      Program using fputs funtion:

    
    #include<stdio.h>
    void main()
    {
        FILE *fp;
    
        fp = fopen("file.txt", "w");
    
        fputs("Welcome to C file handling tutorials......", fp);
        fclose(fp);
    }
    
    


    In above program we used fputs() to write data in a file, fputs() function is used to write string in file and if we use fputc() function then we can able to write only one character in file.


    If we will open the file.txt file then we will see the following output:

    c file handling tutorials, C program to write content in file, c file handling exercises

    •     Above same program using fputc() function:

    #include<stdio.h>
    void main()
    {
        FILE *fp;

        fp = fopen("file.txt""w");

        fputc('a'fp);
        fclose(fp);
    }


    If we will open the file.txt file then we will see the following output:

    C program to create a file and write data in it using fputs and fputc functions




    If you have any doubts please discuss it in the comment section given below. Happy Coding...🙂


    C program using bitwise OR (|)

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

    Input from user:

    a=4 , b=11;

    Expected Output:

    After performing OR operation the output is: 15





      Logic to perform OR operation of two intiger's:


     First you need to know how OR operator works.


    •   The result of OR is 1 if any of the two bits is 1. Otherwise result is 0. 

    1. We take two intiger numbers a=4 and b=11.

     2. Binary representation of a OR b is-


          a      = 0000 0100

          b      = 0000 1011
    ------------------------------------
          a|b   = 0000 1111 =15

    3. After performing OR operation 8 bit binary representation is 0000 1111 which is 15 in decimal.


    4. So output of the program is  15.




      C program using bitwise OR (|) :


    #include<stdio.h>

    void main()
    {
    int a=4,b=11;

    printf("After performing OR operation Output is %d",a|b);

    }

    Above program shows the following output:


    C program using bitwise OR, Write a C program using bitwise OR (|)

    C program using bitwise AND (&)

    Ex: write a C program to perform bitwise AND operation of two intiger's. How to write C Program to perform AND operation of two intiger's. C program to perform AND operation of two intiger's.

    Input from user:

    a=5 , b=15;

    Expected Output:

    After performing AND operation the output is: 5






      Logic to perform AND operation of the two intiger's :


    First you need to know how  AND operator works.


    • The result of AND is 1 only if both bits are 1. Otherwise result is 0.


    1. We take two intiger numbers a=5 and b=15.

    2. The binary representation  of  a & b is:

           a=      0000 0101
           b=      0000 1111
    ---------------------------------
      a&b=      0000 0101 =5

    3. After performing AND operation 8 bit binary representation is 0000 0101 which is 5 in decimal.

    4. So output of the program is  5.





      C program using bitwise AND (&):


    #include<stdio.h>
    void main()
    {
    int a=5, b=15;

    printf("After performing AND operation the output is:%d",a&b);

    }

    Above program shows the following output:


    C program using bitwise AND, Write a C program to perform