Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find the Sum of two Binary Numbers using C#?
Adding two binary numbers in C# involves performing binary arithmetic bit by bit, just like manual addition but working in base-2. The process includes handling carry operations when the sum of bits exceeds 1.
Binary addition follows simple rules: 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 10 (which means 0 with a carry of 1).
Syntax
Following is the basic approach for binary addition −
// Extract rightmost bits bit1 = val1 % 10; bit2 = val2 % 10; // Calculate sum with carry currentSum = (bit1 + bit2 + carry) % 2; carry = (bit1 + bit2 + carry) / 2;
How Binary Addition Works
Using String-Based Binary Addition
This approach treats binary numbers as strings and performs digit-by-digit addition −
using System;
class BinaryAddition {
public static void Main(string[] args) {
long val1, val2, sum = 0;
val1 = 11110;
val2 = 11100;
Console.WriteLine("Binary one: " + val1);
Console.WriteLine("Binary two: " + val2);
sum = DisplaySum(val1, val2);
Console.WriteLine("Sum = {0}", sum);
}
static long DisplaySum(long val1, long val2) {
long i = 0, carry = 0, result = 0;
long[] sum = new long[30];
while (val1 != 0 || val2 != 0) {
sum[i++] = (val1 % 10 + val2 % 10 + carry) % 2;
carry = (val1 % 10 + val2 % 10 + carry) / 2;
val1 = val1 / 10;
val2 = val2 / 10;
}
if (carry != 0)
sum[i++] = carry;
i = i - 1;
while (i >= 0)
result = result * 10 + sum[i--];
return result;
}
}
The output of the above code is −
Binary one: 11110 Binary two: 11100 Sum = 111010
Using Built-in Convert Methods
A more straightforward approach using C#'s built-in conversion methods −
using System;
class BinaryAdditionBuiltIn {
public static void Main(string[] args) {
string binary1 = "11110";
string binary2 = "11100";
Console.WriteLine("Binary one: " + binary1);
Console.WriteLine("Binary two: " + binary2);
// Convert binary to decimal, add, then convert back
int decimal1 = Convert.ToInt32(binary1, 2);
int decimal2 = Convert.ToInt32(binary2, 2);
int decimalSum = decimal1 + decimal2;
string binarySum = Convert.ToString(decimalSum, 2);
Console.WriteLine("Sum = " + binarySum);
// Verification
Console.WriteLine("Decimal equivalent: {0} + {1} = {2}",
decimal1, decimal2, decimalSum);
}
}
The output of the above code is −
Binary one: 11110 Binary two: 11100 Sum = 111010 Decimal equivalent: 30 + 28 = 58
Comparison of Approaches
| Method | Complexity | Best For |
|---|---|---|
| Manual bit-by-bit addition | Higher complexity | Learning binary arithmetic |
| Built-in Convert methods | Lower complexity | Production code, simplicity |
Conclusion
Binary addition in C# can be performed manually using bit-by-bit arithmetic with carry handling, or more simply using built-in conversion methods. The manual approach helps understand binary arithmetic, while the built-in methods provide a cleaner, more maintainable solution for practical applications.
