addition

C code to sum of two integer using Bitwise operator

C code to sum two integer using Bitwise operator

In this article, we will discuss the concept of the C code to sum 0f two integer using Bitwise operator

In this post, we are going to learn how to  write a program to find the sum of two numbers using Bitwise operator in C programming language

Sum of two integer using Bitwise operator

Code to find the addition of  two numbers 

Addition of two integer using Bitwise operator

The program allows the user to enter two integers and then calculates sum of given  numbers using  Bitwise operator in C language

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,num1,num2;

    printf("Enter the first number: \n");
//ask input from the user for num1
    scanf("%d",&num1);
//store the input value in num1

     printf("Enter the second number: \n");
//ask input from the user for num2
    scanf("%d",&num2);
//store the input value in num2
    a=num1;//Assign the value of num1 to a
    b=num2;//Assign the value of num2 to b

    while (num2!= 0)
    {//Calculate the sum of two numbers using bitwise operator
        int bwr=num1 & num2;
        num1=num1^num2;
        num2=bwr<<1;
    }
    printf("Sum of %d and %d is: %d",a,b,num1);
//Display output on the screen
getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the first number:
250
Enter the second number:
350
Sum of 250 and 350 is: 600

 

Addition oft two integer using Bitwise operator – with function

The program allows the user to enter two integers and then calculates sum of given  numbers using  Bitwise operator in C language

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2,ans;
    printf("Enter two numbers: \n");
//ask input from the user for num1 and num2
    scanf("%d %d",&num1,&num2);
//store the input values in num1 and num2
    ans=sum(num1,num2);//Calling the function
    printf("Sum of %d and %d is %d",num1,num2,ans);
//Display output on the screen
    getch();
    return 0;
}
int sum(int x, int y){//function definition
  while(y!=0){//Calculate the sum of two numbers using bitwise operator
    int carry=x & y;
    x=x^y;
    y=carry<<1;
  }
  return x;
}

When the above code is executed, it produces the following result

Enter two numbers
300
100
Sum of 300 and 100 is 400


 

Suggested for you

Operator in C language

Data type in C language

variable in C language

if statements in C language

Function in C language

 

 

Similar post

Subtract two numbers in C language

Subtract two numbers using function in C language

Subtract two numbers using recursion in C language

Find sum of two numbers in C language

Find sum of two numbers in C language using pointer

Find sum of two numbers in C language using function

Find sum of two numbers in C using recursion

Find sum of two numbers in C without Arithmetic operator

Find sum of natural numbers in C language

Find sum of natural numbers in C language using recursion

 

Java Example to sum of two integer using Bitwise operator
C++ code to sum of two integer using Bitwise operator
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago