
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
Add two unsigned numbers using bits in C++.
In this problem, we are given two unsigned numbers, and we need to add them using bits in C++. Bits are binary digits that can be either 0 or 1. Unsigned numbers are positive numbers represented by these bits. To add two unsigned numbers, we add their bits one by one using binary addition rules.
Binary addition works like decimal addition, but with simpler rules:
- 1 + 0 = 1
- 0 + 1 = 1
- 0 + 0 = 0
- 1 + 1 = 0 with a carry of 1
When there is a carry, we add it to the next bit. For example, if the carry is 1, then adding 1 + 1 + carry (1) gives 1 with another carry of 1.
Let's see some examples of adding two numbers using bits:
Scenario 1
Input: a = 5, b = 3 Output: 8 Explanation: Binary of 5 = 0101 Binary of 3 = 0011 Adding bits from right to left with carry: 1 + 1 = 0, carry 1 0 + 1 + carry 1 = 0, carry 1 1 + 0 + carry 1 = 0, carry 1 0 + 0 + carry 1 = 1, carry 0 Final Sum = 1000 (binary) = 8 (decimal)
Scenario 2
Input: a = 21, b = 27 Output: 48 Explanation: Binary of 21 = 10101 Binary of 27 = 11011 Adding bits from right to left with carry: 1 + 1 = 0, carry 1 0 + 1 + carry 1 = 0, carry 1 1 + 0 + carry 1 = 0, carry 1 0 + 1 + carry 1 = 0, carry 1 1 + 1 + carry 1 = 1, carry 1 Extra carry = 1 added at the front Final Sum = 110000 (binary) = 48 (decimal)
Add Two Unsigned Numbers Using Bits
To add two unsigned numbers, we use the bitset class from the <bitset> header, which stores binary data with a fixed number of bits (up to 32 bits).
Algorithm
Following is the algorithm to add two unsigned numbers using bits in C++:
- First, convert the two unsigned numbers into bitset representations.
- Next, create an empty bitset called ctemp to store the result and initialize a carry variable to 0 for handling any carry from the addition.
- Then, use a for loop to go through each bit of the two bitsets, starting from the rightmost (least significant) bit, and apply the binary addition rules to calculate the sum.
- After processing all the bits, if any carry is left, add it to the next bit.
- Finally, use the to_ulong() method of bitset to convert the result (ctemp) back to an unsigned integer and get the final sum.
Example
Below is a C++ program where we follow the above steps to add two unsigned numbers using bits.
#include <bits/stdc++.h> #define M 32 using namespace std; int binAdd(bitset<M> atemp, bitset<M> btemp) { bitset<M> ctemp; for (int i = 0; i < M; i++) ctemp[i] = 0; int carry = 0; for (int i = 0; i < M; i++) { if (atemp[i] + btemp[i] == 0) { if (carry == 0) ctemp[i] = 0; else { ctemp[i] = 1; carry = 0; } } else if (atemp[i] + btemp[i] == 1) { if (carry == 0) ctemp[i] = 1; else { ctemp[i] = 0; carry = 1; } } else { // atemp[i] + btemp[i] == 2 if (carry == 0) { ctemp[i] = 0; carry = 1; } else { ctemp[i] = 1; carry = 1; } } } return ctemp.to_ulong(); } int main() { int a = 678, b = 436; cout << "The sum of " << a << " and " << b << " is "; bitset<M> num1(a); bitset<M> num2(b); cout << binAdd(num1, num2) << endl; return 0; }
The output of the above program is shown below. You can also change the values of a and b in the program to test different cases.
The sum of 678 and 436 is 1114.
Time Complexity: O(M) because the loop runs M times, where M is the number of bits in the bitset (defined by #define M 32).
Space Complexity: O(M) because we store bitsets of size M for both inputs and the result.
Conclusion
In this article, we learned how to add two unsigned numbers using bits in C++. By using the bitset class and applying binary addition rules, we calculate the sum while handling carries. The method has a time and space complexity of O(M), where M is the number of bits.