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


Bitwise Operators In C

Definition: A bitwise operator is an operator used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits.
Bitwise operators are used in communication stack.


Operations can be performed on a bit level using bitwise operators. We use bitwise operators to manipulate data  at its lowest bit level.bitwise operators works on each bit of the data.

There are six bitwise operators are in C programming:

Bitwise AND Operator In C, Bitwise OR Operator In C, Bitwise XOR Operator In C, Bitwise operators in C
Above table shows six bitwise operators in C programming:



1. Bitwise AND(&):

It takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.


[It is used to check perticular bit of data is ON(1) or OFF (0).]

Suppose we will take two intiger variables a & b, where a=9 and b=10.

8 bit binary representation of a & b is...

a= 0000 1001
b= 0000 1010

It will be evaluated as- 


Bitwise AND Operator In C, Bitwise Operators In C


So a & b is evaluated as 0000 1000 which is 8 in decimal.




2. Bitwise OR(|):

It takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.


[ It is commonly used to set flag bit values.]

Suppose we will take two intiger variables a and b, where  a=4 and b=11.

8 bit binary representation of a | b is...

a= 0000 0100
b= 0000 1011

It  is evaluated as-

Bitwise OR Operator In C, Bitwise Operators In C

So a | b is evaluated as 0000 1111 which is 15 in decimal.




3. Bitwise XOR(^):

It takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

Suppose we will take two intiger variables a and b, where a=6 and b=13.

8 bit binary representation of  a^b is....

a= 0000 0110
b= 0000 1101

It is evaluated as-


Bitwise XOR Operator In C, Bitwise operators in C

So a^b is evaluated as 0000 1011 which is 11 in decimal.



4. Bitwise Complement or NOT (~):


It takes one number and inverts all bits of it. Means it  sets bit value 1 only if corresponding bit of operand is 0.


Suppose we take intiger variable a, where a=4.

8 bit binary representation of a is-

a= 0000 0100

It is evaluated as-


Bitwise Complement or Not Operator In C, Bitwise operators in C

So a~ is  evaluated as 1111 1011 which is 124 in decimal.



5. Bitwise left shift(<<):


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


Suppose we take an intiger variable a=15.

8 bit binary representation of a is-

a= 0000 1111

It is evaluated as-


Bitwise Left Shift Operator In C, Bitwise operators in C

Here,  ex: a<<1 means it shifts  bit one times to left which is 30  in decimal. another one example is a<<2 means it shifts bit two times to left which is 60 in decimal.
Left shifting of bit causes insertion of zero from right.




6. Bitwise right shift (>>):


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

Suppose we take intiger variable a=15.

8 bit binary representation of a is-

a= 0000 1111

It is evaluated as-


Bitwise Right Shift Operator In C, Bitwise Operators In C

Here, ex: a>>1 means it shifts bit one time to right which is 7 in decimal. another one example is a>>2 means it shifts bit two time to right which is 3 in decimals.

If you have any queries... feel free to discuss below in the comment box.   
  
Happy Coding....🙃