
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Perform Addition Using Bitwise Operators
Bitwise operators are used for representing binary integers, where the operator directly performs operations on the individual bits of integer values. To perform an addition operation using bitwise operators, use operators like AND, XOR, and NOT. The OR operator cannot perform addition on its own because, 1 | 1 results in 1, but we need 2 as the output. Therefore, you can use the other three operators to implement the logic of addition.
You can see the tabular representation of biwise operators by taking binary bits as 0 and 1.
X | Y | X & Y (AND) | X | Y (OR) | X ^ Y (XOR) | ~(X) [NOT] |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 1 |
0 | 1 | 0 | 1 | 1 | 1 |
1 | 0 | 0 | 1 | 1 | 0 |
1 | 1 | 1 | 1 | 0 | 0 |
Here, we will see the following approaches of bitwise operator to perform addition.
Addition Using Bitwise XOR (^) Operator
The XOR is also known as the exclusive OR operator. It performs logical operations in the form of 0 and 1. 0 means false whereas 1 means true. While using this operator, we iteratively calculate the carry with AND and the sum can be possible without XOR. Next, use the left shift operator (<<) for no remaining carry and display the final addition.
Example
Following is the C++ program that demonstrate addition operation using bitwise XOR(^) Operator.
#include <iostream> using namespace std; int add(int a, int b) { while (b != 0) { // Find carry int carry = a & b; // Sum of bits without carry a = a ^ b; // Shift carry to left b = carry << 1; } return a; } int main() { int x = 13; int y = 29; int result = add(x, y); cout << "Addition of " << x << " and " << y << " using bitwise operators = " << result << endl; return 0; }
The above program produces the following result:
Addition of 13 and 29 using bitwise operators = 42
Addition Using Bitwise NOT (~) Operator
The NOT(~) converts the binary values from 0 to 1 and vice versa. Here, we use the logic of bitwise subtraction and then sum using XOR which calculates the addition without borrow. Next, calculate the borrow that shows the next higher bit as the borrow affects the bit in the left. This way we can perform addition operations using ~ operator.
Example
The following example demonstrates the bitwise NOT(~) operator to perform addition operation.
#include <iostream> using namespace std; int add(int a, int b) { while (b != 0) { // Calculate the borrow int borrow = (~a) & b; // Sum without borrow a = a ^ b; // Shift the borrow left by 1 b = borrow << 1; } return a; } int main() { int a = 5; int b = 3; int result = add(a, b); cout << "Sum: " << result << endl; return 0; }
The above program produces the following result:
Sum: 2
Addition Using Bitwise AND (&) Operator
In the bitwise operator, AND is used to find the carry position. This operator cannot perform the addition alone but it plays a main role by identifying the carry during bitwise addition. Here, the program is similar to the above code [Addition Using Bitwise XOR (^) Operator] but need to remember the below points:
- XOR does the basic addition.
- AND is useful when the carry is needed.
- The left shift operator is useful for carry shifting and added for the next round.
Example
In this example, we perform the bitwise AND operator to solve the addition operation in C++.
#include <iostream> using namespace std; int add(int a, int b) { while (b != 0) { // Calculate the carry int carry = a & b; // Sum without carry a = a ^ b; // Shift the carry left by 1 b = carry << 1; } return a; } int main() { int a = 5; // 0101 int b = 3; // 0011 int result = add(a, b); // This will return 8 (Binary: 1000) cout << "Sum: " << result << endl; return 0; }
The above program produces the following result:
Sum: 8