This is a C Program to perform addition using bitwise operators. Using AND and XOR operators addition can be done, where carry is given by AND between two operands and result can be given by XOR between two operands.
Here is the source code of the C program to perform addition operation using bitwise operators. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include<stdio.h>
int bitwiseadd(int x, int y)
{
while (y != 0)
{
int carry = x & y;
x = x ^ y;
y = carry << 1;
}
return x;
}
int main()
{
int num1, num2;
printf("\nEnter two numbers to perform addition using bitwise operators: ");
scanf("%d%d", &num1, &num2);
printf("\nSum is %d", bitwiseadd(num1, num2));
return 0;
}
$ gcc bitwiseadd.c -o bitwiseadd $ ./bitwiseadd Enter two numbers to perform addition using bitwise operators: 20 12 Sum is 32
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
Related Posts:
- Practice BCA MCQs
- Apply for C Internship
- Practice Computer Science MCQs
- Check Computer Science Books
- Check C Books