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....🙃


C program to print given data in ascending order

Ex: What is the logic to print given number in ascending order. How to print given array elements in ascending order. C program to print given  array elements in ascending order.



   C program to print given data in ascending order:


#include<stdio.h>
void main()
{
int a[]={5,2,7,1,9,10}; 
int i,j,temp=0;
/*Find size of array a*/
  int length=sizeof(a)/sizeof(a[0]);
//Run loops
for(i=0;i<length;i++)
{
for(j=i+1;j<length;j++)
{
if(a[i]>a[j])
{
  /*Use swaping logic*/
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Data in asending order is = ");
for(i=0;i<length;i++)
printf("%d ",a[i]);
}



Above program shows the following output:


C program to print given data in ascending order


C program to print given number is even or odd using bitwise AND operator

Ex: How to find given number is even or odd using bitwise AND operator. C program to print given number is even or odd using bitwise AND operator. What is the logic to print given number is even or odd using bitwise  operator.


Input from user:

Enter the number 
5

Expected Output:

Given number is Odd





   Logic to print given number is even or odd using bitwise operator:


Firstly you need to know how bitwise AND operator works. We can use bitwise AND (&) operator to check given number is even or odd. For example, consider one odd number ( 5 ).  So binary of 5(0101), (5 & 1 = 1). Means least significant of every odd number is 1. Therefore (Odd number & 1) is  always one and also (Even number & 1) is zero.

In Below program we will use bitwise AND operator to check whether given number is even or odd.  [if(no & 1)] means if this condition is true then number is odd otherwise number is even. Because List significant Bit(LSB) of every odd number is 1 and even number is 0. Which is shown above.





   Program to print given number is even or odd using bitwise AND operator:



#include<stdio.h>
void main()
{
int no;
printf("Enter the number\n");
scanf("%d",&no);
if(no & 1)
printf("Given number is Odd\n");
else
printf("Given number is Even\n");
}

Above program shows the following output:


C program to print given number is even or odd using bitwise AND operator


C program to print addition, subtraction, multiplication and division of given two numbers

Ex: Write a C program to print addition,  subtraction, multiplication, division of given two numbers. How to print addition, subtraction, multiplication, division of given two numbers. C program to print addition, subtraction, multiplication, division of given two numbers.

Input from user:
Enter values of a & b: 15  10

Expected output:
Addition is: 25
Subtraction is: 5
Multiplication is: 150
Division is: 1.0000




  Step by step logic of the given program:


1. Accept input two numbers from user declare variable a & b.

2. Add two numbers and store it in some variable say sum, Subtraction of numbers store in variable sub, multiply  two numbers store it in variable mult and last devide number and store it in float variable div.

3. Print sumsubmult and div on the output screen.





  Program to print addition, subtraction, multiplication and division of given two numbers:


/***program to print addition, multiplication, subtraction and division***/

#include<stdio.h>
void main()
{

     int a,b,sum,sub,mult;     
     float div;//variables declaration

      //accept input from user
     printf("Enter the value of a & b\n");
     scanf("%d%d",&a,&b);

 // logic to print addition, subtraction, multiplication and division
     sum=a+b;

     sub=a-b;

     mult=a*b;

     div=a/b;
     

         // display addition
      printf("Addition is %d\n",sum);

      //display subtraction
      printf("subtraction is %d\n",sub);
 
      //display multiplication
      printf("multiplication is %d\n",mult);

     //display division 
      printf("division is %f",div);

}




Above program shows the following output:

program to print addition, subtraction, multiplication and division of given two numbers



Share your thoughts below in the comment section... :)

C Program to Print Division of Given Two Numbers

Ex: Write a C program to accept two numbers from user and display division of this two numbers to the output screen.  How to print division of two numbers. C program to print division of two numbers.

Input from user:
Enter the value of a&b:
20 5

Expected output:
division is: 4.0000



    Step by step logic of the given program:


1.  Accept two numbers from user declare variable say a & b.

2. Devide the number and store division in float variable c.

3. Print division on the output screen.





    C program to print division of given two numbers:


/***C program to print division of two numbers***/

#include<stdio.h>
void main()
{
 int a,b;
 float c;//declaration of variables

  //Get input from user
 printf("Enter the value of a and b\n");
 scanf("%d%d",&a,&b);

 
 c=a/b;//logic for devide two numbers
 
 printf("division is %f",c);//display division
}




Above program shows the following output:


C program to print division of given two numbers