ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT

Add and subtract 2 numbers using bitwise operators. C++ Solution

prodevelopertutorial February 15, 2019

Addition using bitwise operators:

XOR (^) operation will give us addition of 2 bits.
Carry bit can be obtained by performing AND(&) operation.
Finally, to get the final result, we perform (x & y) << 1 and add it to x ^ y to get the result.

The steps can be summarized as below:

while (num_2 != 0)
{
int carry = num_1 & num_2;

num_1 = num_1 ^ num_2;

num_2 = carry << 1;
}

So, why do we have to use carry bit?

Consider below example:

0 + 0 = 0

0 + 1 = 1

1 + 0 = 1

1 + 1 = 0 // generates carry

Here num_1 ^ num_2 will perform the sum of 0+1 and 1+0.

and (num_1 & num_2) << 1 will find all the bits with (1+1) position and since the binary addition is 0, the carry is shifted to next position. As the carry is needed to be added in that position, hence we do those steps.

Subtraction using bitwise operators:

Before looking at the logic, first we shall see simple subtraction example:

1. Without borrow from next bit.

1011011 − 10010 = 1001001

1 0 1 1 0 1 1
− 1 0 0 1 0
1 0 0 1 0 0 1

2. With borrow from next bit

101101 − 100111 = 110:

 

Borrow 1
Borrow 0 0 1
1 0 1 1 0 1
− 1 0 0 1 1 1
1 1 0

 

Hence to perform subtraction we use below code:

while (num_2 != 0) 
{ 
    int borrow = (~num_1) & num_2; 

    num_1 = num_1 ^ num_2; 
    
    num_2 = borrow << 1; 
}

Solution in C++

/*
* File     : bitwise_add_subtract.cpp
* Contact   : ajay.thousand@gmail.com
* Website: @ prodevelopertutorial.com
*/

#include<iostream>
#include<vector>
#include<string>

using namespace std;

int Add(int num_1, int num_2)
{
	while (num_2 != 0) 
	{ 
		int carry = num_1 & num_2;   

		num_1 = num_1 ^ num_2;  

		num_2 = carry << 1; 
	} 
	return num_1;
}

int Subtract(int num_1, int num_2)
{
	while (num_2 != 0) 
	{ 
		int borrow = (~num_1) & num_2; 

		num_1 = num_1 ^ num_2; 

		num_2 = borrow << 1; 
	} 
	return num_1;
}
int main() 
{ 
    cout<<" Addition of 10 and 20 is = "<<Add(10, 20)<<endl; 
  	
  	cout<<" Subtract of 30 and 22 is = "<<Subtract(30, 22)<<endl; 
    return 0; 
}

 

Output:

Addition of 10 and 20 is = 30

 Subtract of 30 and 22 is = 8

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Daily we discuss about competitive programming questions, join us at:   Telegram Channel

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2020 ProDeveloperTutorial.com
Get top courses from: Educative.io