C Program to Perform Addition using Bitwise Operators

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.

  1. #include<stdio.h>
  3. int bitwiseadd(int x, int y)
  4. {
  5.     while (y != 0)
  6.     {
  7.         int carry = x & y;
  8.         x = x ^ y; 
  9.         y = carry << 1;
  10.     }
  11.     return x;
  12. }
  14. int main()
  15. {
  16.     int num1, num2;
  17.     printf("\nEnter two numbers to perform addition using bitwise operators: ");
  18.     scanf("%d%d", &num1, &num2);
  19.     printf("\nSum is %d", bitwiseadd(num1, num2));
  20.     return 0;
  21. }

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


Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.