0

In code I am having a problem I would like to see in the enumerator class a binary format. You know in c# we have the possibility to represent Hexadecimal with 0xFF (f.e.). I would like to know if we have something similar for binary number like:

public static class MyEnum
{
    public static const long TR =       000000001;
    public static const long TRP =      000000010;
    ...
}

This enumerator represent 1, 2, 4, 8, ... for any type I put inside. Just I need a binary number to see easy the number.

How can I represent binary in C#?

Dave
  • 7,028
  • 11
  • 35
  • 58
  • [This question/answer may help] (http://stackoverflow.com/questions/594720/c-sharp-binary-literals) –  Jun 20 '14 at 12:33

4 Answers4

2

If you plan to use only powers of two, the idiomatic way of doing it is by using shifts:

public static const long TR =  1L << 0;
public static const long TRP = 1L << 1;

The L suffix becomes necessary when you shift left by 32 or more positions.

Link: C# does not provide a syntax for binary literals.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Looks there is no build in possibility: http://stackoverflow.com/questions/1246832/c-sharp-binary-constants-representation – vvv Jun 20 '14 at 12:30
  • @vvv I found the same answer too - just added a link. Thanks! – Sergey Kalinichenko Jun 20 '14 at 12:32
  • nice I didn't find the question for this I opened the question, sorry no choice your sample but it is valid for me, I prefer the idea of user user3414693 to see easily the number. I used your code in past, I didnt remember, anyway thanks – Dave Jun 20 '14 at 12:56
2

You can't. Not yet anyway. It will be introduced in C# 6. The syntax will be similar to hexadecimal:

int i = 0b1110000101;

Another great feature that complements this is that you can use underscores in numbers:

int i = 0b11_1000_0101;
Dennis_E
  • 8,751
  • 23
  • 29
2

I dont think there is any such representation in C#

From the ECMA script

9.4.4.2 Integer literals Integer literals are used to write values of types int, uint, long, and ulong. Integer literals have two possible forms: decimal and hexadecimal.

Also check .NET Compiler Platform ("Roslyn")

Probably C# 6.0 will add that feature

C# now tries to help us by introducing a binary literal. Let's start with what we currently have:

var num1 = 1234; //1234
var num2 = 0x1234; //4660

What could possible come now? Here's the answer:

var num3 = 0b1010; //10

Of course binary digits are becoming quite long very fast. This is why a nice separator has been introduced:

var num4 = 0b1100_1010; //202

There can be as many underscores as possible. The underscores can also be connected. And the best thing: Underscores do also work for normal numbers and hex literals:

var num5 = 1_234_567_890; //123456789
var num6 = 0xFF_FA_88_BC; //4294609084
var num7 = 0b10_01__01_10; //150

The only constraint of the underscore is, that of course a number cannot start with it.

Binary literals will make enumerations and bit vectors a little bit easier to understand and handle. It is just more close at what we have been thinking when creating such constructs.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

I'm not sure if I would ever really do this, but here's an idea that just came to me:

    const byte b0001 = 0x1;
    const byte b0010 = 0x2;
    const byte b0100 = 0x4;
    const byte b1000 = 0x8;

    const long Value1 = (// 0b0111
        b0001 |
        b0010 |
        b0100);

    const long Value2 = (// 0b01100111
        b0010 |
        b0100)
        << 4 | (
        b0001 |
        b0010 |
        b0100);

Or you could do something this, which is the same idea but a bit more readable:

    const byte b0110 = 0x6;
    const byte b0111 = 0x7;

    const long Value3 = b0110 << 4 | b0111;
Dr. Wily's Apprentice
  • 10,212
  • 1
  • 25
  • 27