Results 1 to 6 of 6
  1. #1
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using Binary values (VB6)

    Hi,

    I have thought of a potential solution to a problem, and I think I need to use either binary or the binary values (1,2,4,8,16 etc), however, I can't get my head around the maths for using them.

    I have a function that has 10 optional statements to run. I was thinking of using the values 1,2,4,8,16,32,64,128,256 and 512 for each option (1=option 1, upto 512 = option 10) and having a value passed to the function indicating which need to be run.

    i.e. if I call TestFunc(148) options 3, 5 and 8 would run (as 148 is made up from 4 + 16+ 128). However, i'm sure there must be a relativelt simple algorithm for finding out which numbers make up the final number, but im struggling to work out what it is.

    I think the above makes sense, if it doesn't let me know and i'll try and re-write it a bit. Any help would be much appreciated.

    Regards,
    Phil

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Using Binary values (VB6)

    Try this as starting point:

    Sub RunOptions(intValue As Integer)
    Dim i As Integer
    For i = 0 To 15
    If (intValue And 2 ^ i) Then
    ' code for bit i here
    Debug.Print "Option " & (i + 1)
    End If
    Next i
    End Sub

    To test, type RunOptions 148 in the Immediate window and press Enter.

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Using Binary values (VB6)

    Edit: sorry, that was complete rubbish, which will teach me to post while trying to do something else at the same time! I have removed the code, lest it mislead anyone else! <img src=/S/blush.gif border=0 alt=blush width=15 height=15>

    Later edit: having engaged the old grey cells, I now realise it was <code>And</code> rather than <code>Or</code> that I wanted! <img src=/S/doh.gif border=0 alt=doh width=15 height=15>

    <pre>Enum FuncChoices
    fcOption1 = 1
    fcoption2 = 2
    fcOption3 = 4
    fcOption4 = 8
    fcOption5 = 16
    fcOption6 = 32
    fcOption7 = 64
    fcOption8 = 128
    fcOption9 = 256
    fcoption10 = 512
    End Enum

    Function TestChoices(lngInput As Long)
    If lngInput And fcOption1 Then
    MsgBox "Option1 chosen"
    End If
    If lngInput And fcoption2 Then
    MsgBox "Option2 chosen"
    End If
    If lngInput And fcOption3 Then
    MsgBox "Option3 chosen"
    End If
    If lngInput And fcOption4 Then
    MsgBox "Option4 chosen"
    End If
    If lngInput And fcOption5 Then
    MsgBox "Option5 chosen"
    End If
    If lngInput And fcOption6 Then
    MsgBox "Option6 chosen"
    End If
    If lngInput And fcOption7 Then
    MsgBox "Option7 chosen"
    End If
    If lngInput And fcOption8 Then
    MsgBox "Option8 chosen"
    End If
    If lngInput And fcOption9 Then
    MsgBox "Option9 chosen"
    End If
    If lngInput And fcoption10 Then
    MsgBox "Option10 chosen"
    End If

    End Function
    </pre>


    Thus, <code>TestChoices 148</code> should fire a message for option 3, 5 and 8.

    HTH
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using Binary values (VB6)

    Thanks Hans that worked like a treat. Have you got a decription of what ^ does? I tried googling it but it seems to ignor that character.

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Using Binary values (VB6)

    ^ is the "to the power of" operator, for example 2 ^ 5 = 2 to the power 5 = 2*2*2*2*2 = 32.

  6. #6
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using Binary values (VB6)

    Excellent, cool, thanks Hans. Hopefully i'll have the opportunity to make more use of functions that use that in the future - 3 years VB/VBA and this is the first time i've used it!

    Rory, that worked a treat too - I think that was the method I was thinking of when I decided to write my function in this way, I remember seeing a similar one in a piece of code I used (but didnt write) some time ago.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •