2

So, I'm familiar with GCC 0b00010010 for example to write a binary constant. How do I do that in .NET? I'm mainly concerned with VB C# and C++ as I'm debugging/modifying code in those languages.

If there isn't a direct way to do this, is there a short route that won't be impossible to read and modify later?


Code added to illustrate reason for question:

<FlagsAttribute( )> _
Enum ControlTypeEnum as Short
                    ‘   5 4 S 3 2 1 M L
    None = 0        ‘0x 0 0 0 0 0 0 0 0
    Lathe = 1       
    Mill = 2        
    P100 = 4        '*
    P200 = 8        '*
    P300 = 16       
    P300L = 17      '*
    P300SLP = 49    '*
    P300M = 18      '*
    P300SMP = 50    '*
    P400L = 65      '*
    P400SLP = 97    '*
    P400M = 66      '*
    P400SMP = 98    '*
End Enum

'Values with asterisk are valid return values from function
Still.Tony
  • 1,437
  • 12
  • 35

2 Answers2

2

The closest you might be able to do in C# is define it as a string and use Convert.ToInt (or etc.)

 var flags = Convert.ToInt32( "00010010", 2 );

Which is functional, but verbose and pretty bleh. You might (if you are crazy) consider an extension method

public static class Extensions
{
    public static Int32 toInt(this string me)
    {
        return Convert.ToInt32(me, 2);
    }
}
(...)
var myflags = "00010010".toInt();
(...)
Reacher Gilt
  • 1,813
  • 12
  • 26
1

I'd prefer the following:

None = 0                       = 0
Lathe = 1                      = 1
Mill = 2                       = 1<<1
P100 = 4                       = 1<<2
P200 = 8                       = 1<<3
P300 = 16                      = 1<<4
S    = 32                      = 1<<5
P400 = 64                      = 1<<6

... and then, in your code later on, using P400 & Lathe & Mill instead of an extra constant P400LM. (depending on what you like better, use the decimal or bitshift representations of the powers of two.)


If you already have a huge bunch of code depending on those enums and don't want to refactor, you could use

P400LM = 1<<6 | 1<<1 | 1<<0

which makes it clear what this flag is a combination of (much clearer than 67, in my opinion.)

us2012
  • 16,083
  • 3
  • 46
  • 62
  • Ordinarily this is the obvious clean way to code it. Unfortunately most of those aren't valid return values from the function that returns this enumeration. I'm trying to make it so things like `If (GetControlType() AND ControlTypeEnum.S) AND (GetControlType() >= P300)` can be written. I'd have to go through tens of thousands of lines of code and fix every reference to `P300M` for example. +1 for clean solution though. – Still.Tony Sep 12 '13 at 19:21
  • @Okuma.Tony Yeah, I thought this might be a problem, but the second part of my answer should still be applicable, no? – us2012 Sep 12 '13 at 19:22
  • I suppose it would also help to mention this is for an API that is used globally and I can't tell everyone to fix their existing code. It's going to break code as is if anyone was using the old enumeration values instead of calling by enumeration name. – Still.Tony Sep 12 '13 at 19:24
  • 1
    Depending on how much time you have, you might class out a facade around your flags to provide a cleaner interface for valid values. I took a brief look at your company's site and it looks like you're doing some fascinating stuff, but I bet you have major constraints around your project. – Reacher Gilt Sep 12 '13 at 19:26
  • @ReacherGilt Make a static class to replace the enum? – Still.Tony Sep 12 '13 at 19:32