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 product of two binary numbers using C#?
To find the product of two binary numbers in C#, we can implement binary multiplication using the traditional algorithm. This involves multiplying each digit of one binary number with the other and adding the results with proper shifting.
How Binary Multiplication Works
Binary multiplication follows the same principle as decimal multiplication, but uses only 0 and 1. When we multiply by 1, we get the same number; when we multiply by 0, we get zero. The partial products are then added together with appropriate left shifts.
Using String-Based Binary Multiplication
Example
using System;
class BinaryMultiplication {
public static void Main() {
string binary1 = "11100";
string binary2 = "10001";
Console.WriteLine("Binary one: " + binary1);
Console.WriteLine("Binary two: " + binary2);
string product = MultiplyBinary(binary1, binary2);
Console.WriteLine("Product: " + product);
// Verify with decimal conversion
int dec1 = Convert.ToInt32(binary1, 2);
int dec2 = Convert.ToInt32(binary2, 2);
int decProduct = dec1 * dec2;
Console.WriteLine("Verification: " + dec1 + " × " + dec2 + " = " + decProduct);
Console.WriteLine("Decimal " + decProduct + " in binary: " + Convert.ToString(decProduct, 2));
}
static string MultiplyBinary(string bin1, string bin2) {
if (bin1 == "0" || bin2 == "0") return "0";
string result = "0";
int shift = 0;
for (int i = bin2.Length - 1; i >= 0; i--) {
if (bin2[i] == '1') {
string partial = bin1 + new string('0', shift);
result = AddBinary(result, partial);
}
shift++;
}
return result;
}
static string AddBinary(string a, string b) {
string result = "";
int carry = 0;
int i = a.Length - 1;
int j = b.Length - 1;
while (i >= 0 || j >= 0 || carry > 0) {
int sum = carry;
if (i >= 0) sum += a[i--] - '0';
if (j >= 0) sum += b[j--] - '0';
result = (sum % 2) + result;
carry = sum / 2;
}
return result;
}
}
The output of the above code is −
Binary one: 11100 Binary two: 10001 Product: 111011100 Verification: 28 × 17 = 476 Decimal 476 in binary: 111011100
Using Integer-Based Approach
Example
using System;
class Demo {
public static void Main() {
long val1 = 11100;
long val2 = 10001;
long prod = 0;
long digit, factor = 1;
Console.WriteLine("Binary one: " + val1);
Console.WriteLine("Binary two: " + val2);
while (val2 != 0) {
digit = val2 % 10;
if (digit == 1) {
val1 = val1 * factor;
prod = BinaryAdd(val1, prod);
} else {
val1 = val1 * factor;
}
val2 = val2 / 10;
factor = 10;
}
Console.WriteLine("Product = {0}", prod);
}
static long BinaryAdd(long val1, long val2) {
long i = 0, rem = 0, result = 0;
long[] sum = new long[30];
while (val1 != 0 || val2 != 0) {
sum[i++] = (val1 % 10 + val2 % 10 + rem) % 2;
rem = (val1 % 10 + val2 % 10 + rem) / 2;
val1 = val1 / 10;
val2 = val2 / 10;
}
if (rem != 0)
sum[i++] = rem;
i = i - 1;
while (i >= 0)
result = result * 10 + sum[i--];
return result;
}
}
The output of the above code is −
Binary one: 11100 Binary two: 10001 Product = 111011100
Using Built-in Conversion Methods
Example
using System;
class SimpleBinaryMultiplication {
public static void Main() {
string binary1 = "11100";
string binary2 = "10001";
Console.WriteLine("Binary one: " + binary1);
Console.WriteLine("Binary two: " + binary2);
// Convert to decimal, multiply, then back to binary
int decimal1 = Convert.ToInt32(binary1, 2);
int decimal2 = Convert.ToInt32(binary2, 2);
int product = decimal1 * decimal2;
string binaryProduct = Convert.ToString(product, 2);
Console.WriteLine("Product in binary: " + binaryProduct);
Console.WriteLine("Product in decimal: " + product);
}
}
The output of the above code is −
Binary one: 11100 Binary two: 10001 Product in binary: 111011100 Product in decimal: 476
Conclusion
Binary multiplication can be implemented using manual bit manipulation algorithms or by leveraging C#'s built-in conversion methods. The string-based approach provides more control and understanding of the multiplication process, while the conversion method offers simplicity and reliability for practical applications.
