addition

C++ code to sum of two integer using Bitwise operator

C++ code to sum of two integer using Bitwise operator

In this article, we will discuss the concept of the C++ code to sum of 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

Sum of two integer using Bitwise operator

Code to find the addition of  two numbers 

Addition of two integer using Bitwise operator

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 <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int a,b,num1,num2;

    cout<<"Enter the first number: \n";
    cin>>num1;
//Ask the number from the user for num1
    cout<<"Enter the second number: \n";
    cin>>num2;
//Ask the number from the user for 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 count=num1 & num2;
        num1=num1^num2;
        num2=count<<1;
    }
    cout<<"Sum of two numbers "<<a<<" and "<<b<<" is: "<<num1;
//Display output on the screen
getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the first number: 200
Enter the second number: 300
Sum of two numbers 200 and 300 is: 500

 

Sum oft two integer using Bitwise operator

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 <conio.h>
using namespace std;
int sub(int,int);
int main()
{
    int num1,num2,ans;
    cout<<"Enter two numbers: \n";
    cin>>num1>>num2;
//Ask input two integers and store the variables num1,num2
    ans=sub(num1,num2);//Calling the function
//and assign the output to variable ans
    cout<<"Sum of "<<num1<<" and "<<num2<<" is "<<ans;
    getch();
    return 0;
}
int sub(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
125
375
Sum of 125 and 375 is: 500

 

Suggested for you

Operator in C++ language

Data type in C++ language

Variable in C++ language

if statements 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++

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

C code to sum of two integer using Bitwise operator
Python Example to sum of two integer using Bitwise operator
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

2 weeks ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

2 weeks ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

2 weeks ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

4 weeks ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

4 weeks ago

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

7 months ago