# How can we tell the compiler that the input numbers are in Binary Format. RSS

## 8 replies

Last post Jul 10, 2012 03:44 AM by Paul Linton

Member

2 Points

6 Posts

### How can we tell the compiler that the input numbers are in Binary Format.

Hi

I want to add two binary numbers and show the result in Binary. For example, 101 +101 =1010

I can do this in the following steps:

1. Convert the inputs (binary numbers) into Decimal using
`Convert.ToInt64("101", 2)`
3. Convert the decimal  into Binary using
`Convert.ToString(Decimal_Number, 2)`

Is there any way of adding them directly without converting them into intergers?

In fact, I want to know  how we can tell the compiler that the input numbers are in Binary Format?

Regards

All-Star

15346 Points

3142 Posts

### Re: How can we tell the compiler that the input numbers are in Binary Format.

Assalamualikum,

there is no inbuilt function in .net there may be some c# function freely availabe in some blog, please try searching in google and bing with different keywords

All-Star

20513 Points

5712 Posts

### Re: How can we tell the compiler that the input numbers are in Binary Format.

101 +101 =1010

```Int32 hexadecimalSum = 0x01 + 0x02 + 0x03 + 0x04;

output

`10`

Unfortunately, AFAIK, unlike languages that preceding c#,
octal and binary can not be easily represented.

edit:  assembler has binary ... but i can not recall binary in c or c++ ...
because i've worked in many languages, i sometimes
mix my facts ... for c#, it's best to always go to the c# reference:
http://msdn.microsoft.com/en-us/library/618ayhy6

You could write your own function relatively easily.

g.

Member

2 Points

6 Posts

### Re: How can we tell the compiler that the input numbers are in Binary Format.

#### Mudasir.Khan

there is no inbuilt function in .net

Assalamulaikum Mudasir

I wanted a way to specify the binary numbers the way we can specify hexadecimal [in c# as gerry has shown]

#### gerrylowry

Int32 hexadecimalSum = 0x01 + 0x02 + 0x03 + 0x04;

or  octal and hexadecimal [in C/C++].

```int i = 0377;   // Octal constant
int i = 0x3fff;   // Hexadecimal constant```

`Anyway, it seems that you're right.`

Star

13407 Points

2533 Posts

### Re: How can we tell the compiler that the input numbers are in Binary Format.

```string input = "10101";
int y = input.ToCharArray().Aggregate(0, total, x) => total*2+(x=='1'?1:0));
// y == 21```

Error checking is left as an exercise for the reader.

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.

Member

2 Points

6 Posts

### Re: How can we tell the compiler that the input numbers are in Binary Format.

#### Paul Linton

```string input = "10101";
int y = input.ToCharArray().Aggregate(0, total, x) => total*2+(x=='1'?1:0));
// y == 21```

Hi Paul

Doesn't the code shown in my original post do what you've done in a simpler and succinct way?:

Convert the inputs (binary numbers) into Decimal using

`Convert.ToInt64("101", 2)`

I think, C# doesn't provide a way to specify binary nubmers as Mudasir.Khan mentioned earlier. So, I'm going to take his post as an answer to the question. But thank you ALL for your time and nice suggestions.

Star

13407 Points

2533 Posts

### Re: How can we tell the compiler that the input numbers are in Binary Format.

Doesn't the code shown in my original post do what you've done in a simpler and succinct way
The only difference is that my code gives the correct answer and your code gives the incorrect answer!

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.

All-Star

20513 Points

5712 Posts

### Re: How can we tell the compiler that the input numbers are in Binary Format.

#### Paul Linton

my code gives the correct answer

```string input = "10101";
int y = input.ToCharArray().Aggregate(0, total, x) => total*2+(x=='1'?1:0));
// y == 21```

```; expected

Invalid expression term '=>'

Invalid expression term ')'```

ditto vs2010:

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace forums_asp_net_post_5057470_aspx_Paul_Linton
{
class Program
{
static void Main(string[] args)
{
string input = "10101";
int y = input.ToCharArray().Aggregate(0, total, x) => total*2+(x=='1'?1:0));
// y == 21
}
}
}```

vs 2010 error messages:

```Error 1 Invalid expression term '=>'   ...\Program.cs Line 13 column 64
Error 2 ; expected                     ...\Program.cs Line 13 column 64
Error 3 ; expected                     ...\Program.cs Line 13 column 67
Error 4 Invalid expression term ')'    ...\Program.cs Line 13 column 87
Error 5 ; expected                     ...\Program.cs Line 13 column 87```

g.

Star

13407 Points

2533 Posts