
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Perform Addition Operation Using Bitwise Operators
Bitwise operators are used to perform bitwise operations. That implies the manipulation of bits. Some of the bitwise operators are bitwise AND, bitwise OR, bitwise XOR etc.
A program to perform addition operation using bitwise operators is given below −
Example
#include<iostream> using namespace std; int main() { int num1, num2, carry; cout << "Enter first number:"<<endl; cin >> num1; cout << "Enter second number:"<<endl; cin >> num2; while (num2 != 0) { carry = num1 & num2; num1 = num1 ^ num2; num2 = carry << 1; } cout << "The Sum is: " << num1; return 0; }
Output
The output of the above program is as follows −
Enter first number:11 Enter second number: 5 The Sum is: 16
In the above program, the two numbers are obtained from the user. This is given below −
cout << "Enter first number:"<<endl; cin >> num1; cout << "Enter second number:"<<endl; cin >> num2;
After that, addition is carried out using a while loop. It involves using the bitwise AND, bitwise XOR and left shift operators. The code snippet is given below −
while (num2 != 0) { carry = num1 & num2; num1 = num1 ^ num2; num2 = carry << 1; }
Finally, the sum is displayed. This is given below −
cout << "The Sum is: " << num1;
- Related Articles
- How to perform Bitwise Not operation on images using Java OpenCV?
- How to perform Bitwise OR operation on two images using Java OpenCV?
- How to perform Bitwise XOR operation on two images using Java OpenCV?
- How to perform Bitwise And operation on two images using Java OpenCV?
- How to perform bitwise XOR operation on images in OpenCV Python?
- OpenCV Python – How to perform bitwise NOT operation on an image?
- How to perform bitwise AND operation on two images in OpenCV Python?
- How to perform bitwise OR operation on two images in OpenCV Python?
- C program for Addition and Multiplication by 2 using Bitwise Operations.
- Program to perform XOR operation in an array using Python
- How to perform Matrix Addition using C#?
- Python Bitwise Operators
- Java Bitwise Operators
- Perl Bitwise Operators
- Java Program to perform XOR operation on BigInteger

Advertisements