Convert numbers into binary representation and add them without carry

Given two numbers N and M. The task to convert both the numbers in the binary form then add respective bits of both the binary converted numbers but with a given condition that there is not any carry system in this addition.Â
Â
Input: N = 37, M = 12 Output: 41 Input: N = 456, M = 854 Output: 670
Â
Approach:Â
Â
- If we don’t consider carry then the binary addition of two bits will be:Â
Â
1 + 0 = 1
0 + 1 = 1
0 + 0 = 0
1 + 1 = 0 (No carry)
- If you observe clearly you will notice that this is just bitwise XOR of two numbers.Â
Â
Below is the implementation of the above approachÂ
Â
C++
// C++ program to add two binary// number without carry#include<bits/stdc++.h>using namespace std;Â
// Function returns sum of both // the binary number without// carryint NoCarrySum(int N, int M){Â Â Â Â // XOR of N and MÂ Â Â Â return N ^ M;}Â Â Â Â Â // Driver codeint main(){Â Â Â Â int N = 37;Â Â Â Â int M = 12;Â Â Â Â Â Â Â Â Â cout << NoCarrySum(N, M);Â Â Â Â return 0;} |
Java
// Java program to add two binary// number without carryimport java.util.*;Â
class GFG{Â
// Function returns sum of both // the binary number without// carrystatic int NoCarrySum(int N, int M){Â Â Â Â // XOR of N and MÂ Â Â Â return N ^ M;}Â Â Â Â Â // Driver codepublic static void main(String[] args){Â Â Â Â int N = 37;Â Â Â Â int M = 12;Â Â Â Â System.out.print(NoCarrySum(N, M));}}Â
// This code is contributed by amal kumar choubey |
Python3
# Python3 program to add two binary # number without carry Â
# Function returns sum of both # the binary number without # carry def NoCarrySum(N, M): Â
    # XOR of N and M     return N ^ MÂ
# Driver code N = 37M = 12print(NoCarrySum(N, M)) Â
# This code is contributed by sayesha |
C#
// C# program to add two binary// number without carryusing System;Â
class GFG{Â
// Function returns sum of both // the binary number without// carrystatic int NoCarrySum(int N, int M){Â Â Â Â Â Â Â Â Â // XOR of N and MÂ Â Â Â return N ^ M;}Â Â Â Â Â // Driver codestatic public void Main(String[] args){Â Â Â Â int N = 37;Â Â Â Â int M = 12;Â Â Â Â Console.Write(NoCarrySum(N, M));}}Â
// This code is contributed by Rajput-Ji |
Javascript
<script>Â
// Javascript program to add two binary// number without carryÂ
// Function returns sum of both // the binary number without// carryfunction NoCarrySum(N, M){    // XOR of N and M    return N ^ M;}     // Driver code    let N = 37;    let M = 12;         document.write(NoCarrySum(N, M));Â
</script> |
Output:Â
41
Â
Time complexity : O(1)
Auxiliary space : O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


