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
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
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
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…