7

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 I'm 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.

Code:

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'
Bugs
  • 4,491
  • 9
  • 32
  • 41
Wes P
  • 9,622
  • 14
  • 41
  • 48
  • Possible duplicate of [How do I type literal binary in VB.NET?](http://stackoverflow.com/questions/1417662/how-do-i-type-literal-binary-in-vb-net) – Darren Apr 26 '17 at 11:08

5 Answers5

18

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
  • 62,489
  • 19
  • 106
  • 139
4

Unfortunately, the best you can do is Hex:

Dim x As Long = &H1234ABCD
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
2

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
  • 11,334
  • 2
  • 25
  • 33
1

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
  • 11,334
  • 2
  • 25
  • 33
1

Use a comment?

Dim myFantasy as Integer = 2^3 '00000000-00001000
pro3carp3
  • 807
  • 2
  • 7
  • 17