Results 1 to 24 of 24

Thread: And Or bit operation question

  1. #1

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    And Or bit operation question

    I have a custom message box type thing and I'm not sure how to do this. I want to know how to set up the select case statement.

    Anything you can tell me about how to do this is appreciated. I've never understood bitwise comparison, so I always use brute force in these situations - I.e. check every possible combination.

    VB Code:
    1. Public Enum CommandButtons
    2.   cbOK = 0 ' Default ' Should I start with 0 ???
    3.   cbCancel = 1
    4.   cbYes = 2
    5.   cbNo = 4
    6.   cbPrevious = 8
    7.   cbNext = 16
    8.   cbQuestion = 32
    9.   cbHelp = 1024
    10.   cbDontShowAgain = 2048
    11. End Enum
    12.  
    13. Sub DisplayButtons(iValue as long)
    14. ' iValue needs to be split up to display the proper buttons
    15.  
    16. ' How do I write this sub????
    17.  
    18. Select case iValue
    19.  
    20. End Select
    21.  
    22. End Sub

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    From what I understood, you need to know how to use the Select Case statement. I just hope I'm not wrong here, cz I'd look like an idiot.

    Uhm... maybe you should elaborate here, because I just DID write a select case statement, but I think it's not what you want.



    VB Code:
    1. Public Enum CommandButtons
    2.   cbOK = 0 ' Default ' Should I start with 0 ???
    3.   cbCancel = 1
    4.   cbYes = 2
    5.   cbNo = 4
    6.   cbPrevious = 8
    7.   cbNext = 16
    8.   cbQuestion = 32
    9.   cbHelp = 1024
    10.   cbDontShowAgain = 2048
    11. End Enum
    12.  
    13. Sub DisplayButtons(iValue as long)
    14. ' iValue needs to be split up to display the proper buttons
    15.  
    16. ' How do I write this sub????
    17.  
    18. Select case iValue
    19. case cbOK
    20. 'commandok.visible = true or whatever code it is
    21.  
    22. case cbcancel
    23. 'code
    24.  
    25. case cbyes
    26. 'code
    27.  
    28. ... so on
    29.  
    30. End Select
    31.  
    32. End Sub

    This isn't it, is it?

  3. #3

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    No. Because there may be any combination of items used.

    Sorry, I didn't mention that:

    VB Code:
    1. DisplayButtons cbYes + cbNo + cbCancel + cbDontShowAgain + vbHelp

  4. #4

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I think it's something like this

    VB Code:
    1. sub DisplayButtons(iValue as long)
    2.  
    3. cmdOK.Visible = iif(iValue OR cbOK, True, False)
    4. cmdCancel.Visible = iif(iValue OR cbCancel, True, False)
    5. etc.

  5. #5
    Addicted Member
    Join Date
    Apr 2002
    Posts
    235
    Try this

    VB Code:
    1. Dim myNumber As Integer
    2.    
    3.     myNumber = cbyes + cbno
    4.    
    5.     If myNumber And cbyes = cbyes Then
    6.         MsgBox "cbyes"
    7.     End If
    8.    
    9.     If myNumber And cbno = cbno Then
    10.         MsgBox "vbno"
    11.     End If

    Hopefully this will get you off on the right foot. If you want an explanation of why this confusing code works, I'd be happy to explain, but I wanted to let you get your hands on something you can use.

    You probably don't want to use 0 if you want to be able to combine an OK button with other buttons. You could still have 0 as the default by shecking for 0 when you enter the sub, and if ivalue is 0, use only the OK button.

  6. #6
    Addicted Member
    Join Date
    Apr 2002
    Posts
    235

    wait!

    Ugh, that doesn't work....hang on...

  7. #7

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Thanks. That's what I was looking for. I'll drop the 0 because I thought I might need to start with 1.

    If would like to explain it, feel free. Unfortunately, I've already had it explained to me a half-dozen times and it just hasn't stuck. I think the main reason for that is that I've never really used it. I could use a refresher I guess

    Thanks again.

  8. #8

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: wait!

    Originally posted by csnyder
    Ugh, that doesn't work....hang on...
    *hanging*

  9. #9
    Addicted Member
    Join Date
    Apr 2002
    Posts
    235
    OK, it works. Just make sure you put parenthesis around the bit comparison:

    VB Code:
    1. Dim myNumber As Integer
    2.    
    3.     myNumber = cbyes + cbno
    4.    
    5.     If (myNumber And cbyes) = cbyes Then
    6.         MsgBox "cbyes"
    7.     End If
    8.    
    9.     If (myNumber And cbno) = cbno Then
    10.         MsgBox "vbno"
    11.     End If
    12.    
    13.     If (myNumber And cbprevious) = cbprevious Then
    14.         MsgBox "and cbprevious"
    15.     End If

    otherwise, it will evaluate myNumber And cbprevious = cbprevious as myNumber And (cbprevious = cbprevious) which will always be true.

  10. #10

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Great. Works perfectly. Thanks a lot.

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Suggestion:
    (As from Mendhak)

    You could still use Select Case to tidy thins up, and loose the If stuff.

    ie.
    VB Code:
    1. Select Case True
    2.     Case (myNumber And cbYes)
    3.         MsgBox "cbyes"
    4.    
    5.     Case (myNumber And cbNo)
    6.         MsgBox "vbno"
    7.    
    8.     Case (myNumber And cbPrevious)
    9.         MsgBox "cbprevious"
    10. End Select

  12. #12

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    actually, this is what I'm doing.

    VB Code:
    1. Sub DisplayButtons(iVal As Long)
    2.  
    3. cmdYes.Visible = IIf((iVal And cbYes) = cbYes, True, False)
    4. cmdNo.Visible = IIf((iVal And cbno) = cbno, True, False)
    5.  
    6.  
    7. End Sub

  13. #13
    Addicted Member
    Join Date
    Apr 2002
    Posts
    235

    an explanation

    The key to understanding bit-wise comparison is understanding what AND and OR evaluate to when it looks at bits. AND makes a bit 1 if the 2 bits it compares are 1. OR makes a bit 1 if either bit is one. You’ve probably seen this table.

    Code:
    
    Bit    Bit
     1       2    AND    OR 
     1       1     1     1
     1       0     0     1
     0       1     0     1
     0       0     0     0
    Once you’ve got this down, you should be able to figure out when to use AND and when to use OR. Here are the binary values for your CammandButtons (put in a code block to preserve spacing)

    Code:
    cbcancel   = 1 = 00000001
    cbyes      = 2 = 00000010
    cbno       = 4 = 00000100
    cbprevious = 8 = 00001000
    and so on….
    Now, if you add 2 or more of the values together, each bit in the sum is a 1 if it was a 1 in either of the values.

    Cbyes + cbno = 6 = 00000110

    So the trick is to check each bit in ivalue to see whether it is a 1 or a 0. To do this, you need a mask, which is a number that isolates the bit you care about. You compare your value to the mask using the AND operation. Take a look at cbyes + cbno And cbyes:
    Code:
    Cbyes + cbno = 00000110   And
    Cbyes        = 00000010
    
    Result       = 00000010
    If the result equals the mask, you know that cbyes was specified.

    Here’s a quick example of how it works if the mask isn’t in the value. The expression is cbyes + cbno And cbprevious
    Code:
    Cbyes + cbno = 00000110   And
    Cbprevious   = 00001000
    
    Result       = 00000000
    No bit is 1 in both of the values, so the result is 0. Thus, you know that cbprevious wasn’t specified.

    Hope this helps make things clearer, and not murkier.

  14. #14

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Thank you. It makes it very clear. I need to print it out and paste it to my forehead though. I really appreciate it. Now I'm busy figuring out how to get the buttons to line up right. I've got a handle on that though. I'll post it once I've got it.

  15. #15
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Any particular settings? I had to move frmmessage to a new folder named "Common VB Code" and then I get this message

    Wrong Number of arguments or invalid property assigned
    on this line in sub_main()


    Code:
    iReturn = frmMessage.ShowMessage(0, cbYes + cbNo, False, "Message Box")
    (.showmessage) is highlighted. So I'm assuming something's missing here.

  16. #16

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Don't sweat it. I posted the wrong thing. What I posted was old dicked up code. Just delete it. I'll post the right code when I get it. I'm going to delete that thread. Sorry.

  17. #17
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    dang.

  18. #18

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    There are TWO things that I REALLY, REALLY HATE about VB!

    1) When you right click a module and then click Save As... it doesn't replace the module in your project. For example, when you tell it to save the module in a different folder, the project still uses the module in the original folder. I've lost a lot of code because I forget about that. You actually have to remove the module and then load the one you want to use.

    2) Whenever you have a module maximized and then run your code and then end the app, VB minimizes the window you had open and maximizes one of its windows, like the properties window, for example. That is just so extremely irritating.

    OK... here's the code with the right modules
    Attached Files Attached Files

  19. #19
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    OK, I get a message box with cafeenman propaganda spewed on it, and if I click on "Do not show this message again" and click Yes, it returns 16. Fine so far.

    The next time I run it, I get the same propagandabox again. Does it work for you?

  20. #20

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by mendhak
    OK, I get a message box with cafeenman propaganda spewed on it, and if I click on "Do not show this message again" and click Yes, it returns 16. Fine so far.

    The next time I run it, I get the same propagandabox again. Does it work for you?
    No, I haven't done anything with the "Don't Show Again" bit yet. Really I just posted this code to show how I did the buttons. That's the whole point of this thread.

    In other words, I didn't post the code so you can slap it into an app. I mean it's cool if you want to do that, but there's more work to be done to make this complete. I just wanted a better way to display the right buttons without a 40 case, Select Case block of code.

  21. #21
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by cafeenman
    actually, this is what I'm doing.

    VB Code:
    1. Sub DisplayButtons(iVal As Long)
    2.  
    3. cmdYes.Visible = IIf((iVal And cbYes) = cbYes, True, False)
    4. cmdNo.Visible = IIf((iVal And cbno) = cbno, True, False)
    5.  
    6.  
    7. End Sub
    You don't need the Iif function. Just try this:
    VB Code:
    1. Sub DisplayButtons(iVal As Long)
    2.  
    3. cmdYes.Visible = ((iVal And cbYes) > 0)
    4. cmdNo.Visible = ((iVal And cbno) > 0)
    5.  
    6.  
    7. End Sub
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  22. #22
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by Bruce Fox
    Suggestion:
    (As from Mendhak)

    You could still use Select Case to tidy thins up, and loose the If stuff.

    ie.
    VB Code:
    1. Select Case True
    2.     Case (myNumber And cbYes)
    3.         MsgBox "cbyes"
    4.    
    5.     Case (myNumber And cbNo)
    6.         MsgBox "vbno"
    7.    
    8.     Case (myNumber And cbPrevious)
    9.         MsgBox "cbprevious"
    10. End Select
    That's not correct. This way you should get one message, if myNumber = cbYes + cbNo + cbprevious, when you would get three messages if you have three different if statements.
    Last edited by Mc Brain; May 6th, 2002 at 04:41 AM.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  23. #23

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    lol. I didn't end up doing the IIF thing. Here's what I ended up with:

    The index of the buttons correspond to the Enum.

    VB Code:
    1. Private Sub ShowButtons(iButtons As Long)
    2. Dim i As Integer
    3. Const iSpace As Long = 60
    4. Dim iLeft As Long
    5.  
    6. iLeft = ScaleWidth - 60
    7. i = 1
    8.  
    9. Do
    10.   With cmdButton(i)
    11.     If (iButtons And i) = i Then
    12.       iLeft = iLeft - (cmdButton(1).Width + iSpace)
    13.       .Left = iLeft
    14.       .Visible = True
    15.     Else
    16.       .Visible = False
    17.     End If
    18.   End With
    19.   i = i * 2
    20. Loop Until i > cbPrevious
    21.  
    22. End Sub
    You'll have to see the rest of the code to see what the different
    values are if you care. cbPrevious is the last button.

  24. #24
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    In that case, OK.

Posting Permissions

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



Click Here to Expand Forum to Full Width