

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to find the Sum of two Binary Numbers using C#?
To find the sum of two binary numbers, firstly set them.
val1 = 11110; val2 = 11100;
Now call the displaySum() method, which created to display the sumL.
sum = displaySum(val1, val2);
We have set a new array in the method to display each bit of the binary number.
long[] sum = new long[30];
Now let us see the complete code to calculate the sum of binary numbers as shown in the code below −
Example
using System; class Demo { 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, rem = 0, res = 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) res = res * 10 + sum[i--]; return res; } }
Output
Binary one: 11110 Binary two: 11100 Sum = 111010
- Related Questions & Answers
- How to find the product of two binary numbers using C#?
- Find the Sum of two Binary Numbers without using a method in C#?
- How to Find the Sum of Natural Numbers using Python?
- C Program to find sum of two numbers without using any operator
- C program to find sum and difference of two numbers
- Find the overlapping sum of two arrays using C++
- Java Program to Find the Sum of Natural Numbers using Recursion
- How to Find Sum of Natural Numbers Using Recursion in Python?
- Python Program to Find the Product of two Numbers Using Recursion
- Java Program to Find the Product of Two Numbers Using Recursion
- C++ program to Find Sum of Natural Numbers using Recursion
- Java Program to Find Sum of N Numbers Using Recursion
- Find LCM of two numbers
- Find GCD of two numbers
- Program to find sum of two numbers which are less than the target in Python
Advertisements