- On
- By
- 0 Comment
- Categories: addition, Calculations
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
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
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