views:

1227

answers:

5

Is there a way to assign a binary value to a vb variable? All of the obvious choices don't work. I've tried prefixing with &B, appending with b but nothing seems to work. I'm not having much luck searching for it either. I don't NEED this for my application, but am rather, just curious so alternate solutions are not necessary.

[Edit] For clarification, what I was looking for (which does not appear to be possible) was how to assign a literal binary number to a variable, in a similar manner in which you can assign a hex or octal number. I was just looking for a more visually engaging way to assign values to a flag enum.

Dim num as Integer = &H100ABC       'Hex'
Dim num2 as Integer = &O123765      'Octal'

Dim myFantasy as Integer = &B1000   'What I would like to be able to do'
Dim myReality as Integer = 2 ^ 3    'What I ended up doing'

Thanks!

+2  A: 

Unfortunately, the best you can do is Hex:

Dim x As Long = &H1234ABCD
Joel Coehoorn
+8  A: 

I might stab myself after this one:

Convert.ToInt32("1100101", 2);

On a more serious note (noticing that you have updated the question), for flag enums what you may want is the left shift operator (<<):

Dim myReality as Integer          = 1 << 0   // 1
Dim myAlternateReality as Integer = 1 << 1   // 2
Dim myParallelUniverse as Integer = 1 << 2   // 4
...

and so on up to 31.

John Rasch
+1 - for a correct answer. Gave me a chuckle, Thanks :)
Wes P
+1 "runs howling into the night"
Denis Troller
+1  A: 

I assume you mean what John Rasch provided an answer for. You have a string composed of zeroes and ones that you want converted to some kind of variable.

    Dim b As String = "10101"
    Dim i As Integer = 73
    Dim s As String
    s = Convert.ToString(i, 2) 's contains the binary representation of 73 - 1001001
    i = Convert.ToInt32(b, 2) 'i now =  21

and you can also use these methods for hex(16) and octal(8).

you could also do something like this

    Dim i As Integer = 6
    If (i And _bi.t0) = _bi.t0 OrElse (i And _bi.t1) = _bi.t1 Then
      'bit 0 or 1 on
    End If

Enum _bi As Integer
    t0 = CInt(2 ^ 0)
    t1 = CInt(2 ^ 1)
    t2 = CInt(2 ^ 2)
    t3 = CInt(2 ^ 3)
    t4 = CInt(2 ^ 4)
    t5 = CInt(2 ^ 5)
    t6 = CInt(2 ^ 6)
    t7 = CInt(2 ^ 7)
    t8 = CInt(2 ^ 8)
    t9 = CInt(2 ^ 9)
    t10 = CInt(2 ^ 10)
    t11 = CInt(2 ^ 11)
    t12 = CInt(2 ^ 12)
    t13 = CInt(2 ^ 13)
    t14 = CInt(2 ^ 14)
    t15 = CInt(2 ^ 15)
    t16 = CInt(2 ^ 16)
    t17 = CInt(2 ^ 17)
    t18 = CInt(2 ^ 18)
    t19 = CInt(2 ^ 19)
    t20 = CInt(2 ^ 20)
    t21 = CInt(2 ^ 21)
    t22 = CInt(2 ^ 22)
    t23 = CInt(2 ^ 23)
    t24 = CInt(2 ^ 24)
    t25 = CInt(2 ^ 25)
    t26 = CInt(2 ^ 26)
    t27 = CInt(2 ^ 27)
    t28 = CInt(2 ^ 28)
    t29 = CInt(2 ^ 29)
    t30 = CInt(2 ^ 30)
End Enum
dbasnett
And I thought that I was committing code treason
John Rasch
A: 

or something descriptive

Enum _SerialPortPins As Integer
    RTS = CInt(2 ^ 4)
    CTS = CInt(2 ^ 5)
    DSR = CInt(2 ^ 6)
    DCD = CInt(2 ^ 8)
    DTR = CInt(2 ^ 20)
    RI = CInt(2 ^ 22)
End Enum
dbasnett
A: 

Use a comment?

Dim myFantasy as Integer = 2^3 '00000000-00001000
pro3carp3