Consulting

Results 1 to 12 of 12

Thread: Operators in Arguments question

  1. #1
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location

    Operators in Arguments question

    When using logical operators, such as Or (or) And in a common manner, such as:
    [vba]
    Sub exa11()
    Dim lNumOne As Long, lNumTwo As Long

    lNumOne = CLng(InputBox("Enter Number", vbNullString))
    lNumTwo = CLng(InputBox("Enter Number", vbNullString))

    If lNumOne > 5 Or lNumTwo > 10 Then
    MsgBox "Passed"
    Else
    MsgBox "Failed"
    End If
    End Sub
    [/vba]
    ...if a number exceeding 5 is entered into the first input, OR, a number exceeding 10 is placed in the second, the overall test is passed, as only one of the conditions must be met for a True to be returned. This I get.

    However, when I have used snippets of examples (in my case, usually API related), I have used expressions such as:
    [vba]
    Public Function TitleBar_Hide(UF As Object, lUFHandle As Long)
    Dim lWindow As Long

    lUFHandle = FindWindowA(vbNullString, UF.Caption)
    lWindow = GetWindowLong(lUFHandle, GWL_STYLE)
    lWindow = lWindow And (Not WS_CAPTION)
    Call SetWindowLong(lUFHandle, GWL_STYLE, lWindow)
    Call DrawMenuBar(lUFHandle)
    End Function
    [/vba]
    or...
    [vba]
    lngRet = InsertMenu(hMenuPopMisc, 1&, MF_POPUP Or MF_BYPOSITION Or MF_ENABLED, hMenuPop, "Viewable Months")
    [/vba]
    ...where argument 3 has 3 different constants listed.

    I could not quickly find an example (so hope I don't botch), but it seems to me what I've seen a number of times is similar to:
    [vba]
    lWhatever = lSomeVariable Or A_Constant
    [/vba]

    My question is particularly to what is the Or doing, but I would appreciate if what is happening with 'lWindow And (Not WS_CAPTION)' is touched on.

    If this would require an overly lengthy explanation, keywords to google by or suggestions as to what to read would be great.

    As always and of course, thank you so much,

    Mark

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,877
    I'm not sure, but don't they just resolve to TRUE or FALSE being passed as an argument?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    These are bit operators Mark. It is manipulating the bits of the variables to check particualr bit settings. So for instance

    1 OR 2 returns 3
    2 OR 2 returns 2

    so you can see it compares the corresponding bits of two values, if either bit is 1, the result is 1, if both bits are 0, the result is 0.

    There are similar, although obviously different, rules for AND, XOR, EOR and so on.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    There is certainly so much that I have to learn. Sigh... Shucks, to me, a Pointer is a dog one would take to hunt winged quarry.

    Quote Originally Posted by xld
    so you can see it compares the corresponding bits of two values, if either bit is 1,...
    Me, "Yeh? Not so much!" (as to me "seeing" it at first)

    I fully admit that this was not making a single bit (<no pun intended) of sense to me. To explain, I think you were believing that I had some grasp on binary numbers; again, "...not so much".

    Worse yet, the one time I recall seeing any type of explanation of Or being used between two or more Constants was when a fellow wrote (in a thread I was helping in) something like:
    [vba]MsgBox "Hello World", vbDefaultButton2 Or vbExclamation Or vbOKCancel, ""[/vba]

    When I mentioned that I thought the constants should be seperated by plus (+) signs, he courteously acknowledged that this might be more intuitive, but was really the same thing. (You are probably ahead of me by this point...)

    So...the blonde guy, still thinking in decimal numbers, confirms by trying [vba]x = vbDefaultButton2 Or vbExclamation Or vbOKCancel[/vba] and by golly if x doesn't return 305. You see where this went in my head...

    So these binary number thingys are important to code?

    Okay, I read here:
    http://www.math.grin.edu/%7Erebelsky...ry.html#1011-q
    (not in entirety yet, but through addition)
    as well as here:
    http://www.excely.com/excel-vba/bit-operations.shtml

    It took me a while to get my head wrapped around this, as I just wasn't 'getting' verbiage like, "Bitwise operator OR check if either the right operand or the left operand is true, or if they are both true" or "manipulating the bits of the variables to check particualr bit settings" (Sorry). Finally though, your "compares the corresponding bits of two values," sunk in (at least I am hoping awfully much!)

    So, if you don't mind, let's see how I'm doing in at least getting the basics:

    'Bitwise' could be defined as referring to each individual bit in a binary number. Thus, a bitwise operation is operating on each column of two or more binary numbers.

    1 OR 2 returns 3 because:

    [vba]
    Decimal Binary
    1 = 1
    2 = 10
    ------
    (OR results in) 11 which expressed in decimal would be 3
    [/vba]

    Presuming I'm hanging in so far, if we extend this to three values, such as the Constants used in the Buttons argument:

    256 OR 48 OR 1 returns 305 because:

    [vba]
    Decimal Binary
    256 = 100000000
    48 = 110000
    1 = 1
    ----------
    OR results in 100110001
    [/vba]
    which is 305, as 305 = (1*2^8)+(0*2^7)+(0*2^6)+(1*2^5)+(1*2^4)+(0*2^3)+(0*2^2)+(0*2^1)+(1*2^0)

    Phew!

    Mark

    PS - If your eyes haven't glazed over, another question or two. This was just as much as I could get before mine did. And BTW, besides being a nice donkey in Winnie the Poo, what is EOR?

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by GTO
    And BTW, besides being a nice donkey in Winnie the Poo, what is EOR?
    Too good an opportunity!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by GTO
    There is certainly so much that I have to learn. Sigh...
    ... and so little time

    Quote Originally Posted by GTO
    Me, "Yeh? Not so much!" (as to me "seeing" it at first)

    I fully admit that this was not making a single bit (<no pun intended) of sense to me. To explain, I think you were believing that I had some grasp on binary numbers; again, "...not so much".
    Yes I admit that was an assumption, I thought they always taught that at computer class. When I was at uni, we were taught to use an octal based number system, it is the most logical number base, but I guess we have lost that one in converting the world

    Quote Originally Posted by GTO
    Worse yet, the one time I recall seeing any type of explanation of Or being used between two or more Constants was when a fellow wrote (in a thread I was helping in) something like:
    [vba]MsgBox "Hello World", vbDefaultButton2 Or vbExclamation Or vbOKCancel, ""[/vba]

    When I mentioned that I thought the constants should be seperated by plus (+) signs, he courteously acknowledged that this might be more intuitive, but was really the same thing. (You are probably ahead of me by this point...)
    That clearly wasn't the only time, you yourself pointed out the usage in checking return values from API dunctions.

    That guy was (is) very astute, he is correct, and nice to hear he was courteous and didn't come over all pompous developer. I used to use OR in that way in MsgBox buttons and the like, but nobody else does, and I have changed to +. I wish I hadn't.

    Quote Originally Posted by GTO
    So...the blonde guy, still thinking in decimal numbers, confirms by trying [vba]x = vbDefaultButton2 Or vbExclamation Or vbOKCancel[/vba] and by golly if x doesn't return 305. You see where this went in my head...

    So these binary number thingys are important to code?
    Wow Mark, you have realised that binary numbers are important to computers (never mind code) ... you are quick

    Quote Originally Posted by GTO
    Okay, I read here:
    http://www.math.grin.edu/%7Erebelsky...ry.html#1011-q
    (not in entirety yet, but through addition)
    as well as here:
    http://www.excely.com/excel-vba/bit-operations.shtml

    It took me a while to get my head wrapped around this, as I just wasn't 'getting' verbiage like, "Bitwise operator OR check if either the right operand or the left operand is true, or if they are both true" or "manipulating the bits of the variables to check particualr bit settings" (Sorry). Finally though, your "compares the corresponding bits of two values," sunk in (at least I am hoping awfully much!)
    Just remember that 0 is FALSE, 1 is TRUE (actually, in pure coding terms, -1 is TRUE, but we can pass over that).

    Quote Originally Posted by GTO
    So, if you don't mind, let's see how I'm doing in at least getting the basics:

    'Bitwise' could be defined as referring to each individual bit in a binary number. Thus, a bitwise operation is operating on each column of two or more binary numbers.

    1 OR 2 returns 3 because:

    [vba]
    Decimal Binary
    1 = 1
    2 = 10
    ------
    (OR results in) 11 which expressed in decimal would be 3
    [/vba]

    Presuming I'm hanging in so far, if we extend this to three values, such as the Constants used in the Buttons argument:

    256 OR 48 OR 1 returns 305 because:

    [vba]
    Decimal Binary
    256 = 100000000
    48 = 110000
    1 = 1
    ----------
    OR results in 100110001
    [/vba]
    which is 305, as 305 = (1*2^8)+(0*2^7)+(0*2^6)+(1*2^5)+(1*2^4)+(0*2^3)+(0*2^2)+(0*2^1)+(1*2^0)
    That is right.

    Similalrly, AND returns 1 (TRUE) when both oprands are 1 (TRUE). So

    1 AND 0 returns 0
    1 and 1 returns 1
    10 and 11 returns 10

    and so on .

    And XOR (Exclusive OR) returns a 1 (TRUE) if only one of the operands has a value of 1 (TRUE ). So

    10 XOR 10 returns 0
    10 XOR 11 returns 1
    0 XOR 11 reurns 11

    Extending it to multiple operands
    1 XOR 0 returns 1,
    whereas 1 XOR 0 XOR 1 returns 0

    Bit operators are a tad esoteric I admit, but we do come across them in VBA, in APIs as you have found, but also in MsgBox buttons as you described above. I have often used them to have multiple values in a single variable, say set the value 1, 2, 4,16,32, etc for different conditions, and then you use bit operators to test if any combination of conditions has been met.

    Quote Originally Posted by GTO
    PS - If your eyes haven't glazed over, another question or two. This was just as much as I could get before mine did. And BTW, besides being a nice donkey in Winnie the Poo, what is EOR?
    Actually, the donkey is Eeyore, very different. EOR is the same as XOR, XOR is what VB supports, but it can also mean an Exclusive OR gate in loigic design, but that is beyond this chat.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by mdmackillop
    Too good an opportunity!
    I have to say thank-you. One of the downsides of being stuck on nights for the past several months is that one barely gets out on the weekends due to sleep patterns being so screwed up.

    Anyways, I did get out tonight, just for a bit, have a couple of brews, come home and checked the site. I actually hit ctrl+end to quick to see it at first, but when scrolled up... suffice it to say that I am still wiping my eyes and damn near peed myself laughing!

    That just kills!

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Glad you like it!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,738
    Location
    Another place that bit-wise operators and mappings frequent arise is with file attributes: From Help (Attributes)

    [vba]
    Option Explicit

    'Normal 0 Normal file. No attributes are set.
    'ReadOnly 1 Read-only file. Attribute is read/write.
    'Hidden 2 Hidden file. Attribute is read/write.
    'System 4 System file. Attribute is read/write.
    'Volume 8 Disk drive volume label. Attribute is read-only.
    'Directory 16 Folder or directory. Attribute is read-only.
    'Archive 32 File has changed since last backup. Attribute is read/write.
    'Alias 64 Link or shortcut. Attribute is read-only.
    'Compressed 128 Compressed file. Attribute is read-only.
    Sub SetClearArchiveBit()
    Dim oFSO As Object, oFile As Object
    Dim iAns As Long, iAttributes As Long, iHSA As Long
    Dim sFileName As String

    sFileName = Application.GetOpenFilename("Any File (*.*), *.*")
    If sFileName = "False" Then Exit Sub

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFile = oFSO.GetFile(oFSO.GetFileName(sFileName))

    iHSA = vbHidden Or vbSystem Or vbArchive

    If (oFile.Attributes And vbNormal) Then MsgBox "Normal"
    If (oFile.Attributes And vbReadOnly) Then MsgBox "ReadOnly"
    If (oFile.Attributes And vbHidden) Then MsgBox "Hidden"
    If (oFile.Attributes And vbSystem) Then MsgBox "System"
    If (oFile.Attributes And vbVolume) Then MsgBox "Volume Label"
    If (oFile.Attributes And vbDirectory) Then MsgBox "Directory"
    If (oFile.Attributes And vbArchive) Then MsgBox "Archive"
    If (oFile.Attributes And vbAlias) Then MsgBox "Alias"
    If (oFile.Attributes And 128) Then MsgBox "Compressed"

    'desktop.ini
    If (oFile.Attributes And iHSA) Then MsgBox "The Hidden, System, and Archive bits are set"

    If (oFile.Attributes And vbArchive) Then
    iAns = MsgBox("The Archive bit is set, do you want to clear it?", vbYesNo, "Set/Clear Archive Bit")
    If iAns = vbYes Then
    oFile.Attributes = oFile.Attributes And Not vbArchive
    MsgBox "Archive bit is cleared."
    Else
    MsgBox "Archive bit remains set."
    End If
    Else
    iAns = MsgBox("The Archive bit is not set. Do you want to set it?", vbYesNo, "Set/Clear Archive Bit")
    If iAns = vbYes Then
    oFile.Attributes = oFile.Attributes Or vbArchive
    MsgBox "Archive bit is set."
    Else
    MsgBox "Archive bit remains clear."
    End If
    End If
    End Sub
    [/vba]

    Paul

  10. #10
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi Bob :-)

    Utter apologies, I had gotten about halfway through responding and it was like a truck ran over me. Its like your body just decides to make up for all the lost sleep at once; 11 hours!

    Anyways, here we go!

    Quote Originally Posted by xld
    ... and so little time
    OUCH! Seeing as you can probably get Excel to fetch the paper and warm up the car, I'm a little scared. I'm only 49, you know something I don't?!

    Quote Originally Posted by xld
    That clearly wasn't the only time, you yourself pointed out the usage in checking return values from API dunctions.

    That guy was (is) very astute, he is correct, and nice to hear he was courteous and didn't come over all pompous developer. I used to use OR in that way in MsgBox buttons and the like, but nobody else does, and I have changed to +. I wish I hadn't.
    I don't quite catch the first part, but yes, I had realized that the OR wasn't truly adding the vals. It had just become confusing, as once I had (unfortunately) concluded wrongly, the 'bad thought' just seemed sort of stuck there.

    As to the second part, seriously (but only for a moment), as I've mentioned when 'chatting,' one of the nice discoveries for me was how helpful and patient knowledgeable folks tend to be. While the world has plenty of 'issues', and the people I deal with daily lead one to become rather 'hard' or cynical in viewing others, this site and others reminds me of the goodness that is possible.

    Quote Originally Posted by xld
    Wow Mark, you have realised that binary numbers are important to computers (never mind code) ... you are quick
    Hey, no double-teaming outta you and Malcom! Yes though, I can be awfully thick somedays

    Quote Originally Posted by xld
    Just remember that 0 is FALSE, 1 is TRUE (actually, in pure coding terms, -1 is TRUE, but we can pass over that).
    Yes sir.

    Quote Originally Posted by xld
    10 XOR 10 returns 0
    10 XOR 11 returns 1
    0 XOR 11 returns 11
    ACK!, will have to study that.
    Quote Originally Posted by xld
    Bit operators are a tad esoteric I admit, but we do come across them in VBA, in APIs as you have found, but also in MsgBox buttons as you described above. I have often used them to have multiple values in a single variable, say set the value 1, 2, 4,16,32, etc for different conditions, and then you use bit operators to test if any combination of conditions has been met.
    Thank you. This seems beyond me at the moment. Not saying I wouldn't make sense of it; just will have to see an example.

    Quote Originally Posted by xld
    Actually, the donkey is Eeyore, very different. EOR is the same as XOR, XOR is what VB supports, but it can also mean an Exclusive OR gate in loigic design, but that is beyond this chat.
    Picky schnicky; you know you were laughing :-) As it wasn't in help, I Bing'd it. Figured it wasn't #1 (Enhanced oil recovery) and #2 wasn't likely (a personal web page), so had to go with #3 (Winnie the Pooh - The Official Site) - and yes, I typed in only EOR

    Quote Originally Posted by xld
    Yes I admit that was an assumption, I thought they always taught that at computer class. When I was at uni, we were taught to use an octal based number system, it is the most logical number base, but I guess we have lost that one in converting the world
    I saved this for last.

    Well, I'm sure they do teach this in computer classes, but I could not attest one way or the other. Ya'll are my 'computer class' :-)

    I would like to take a class or two whenever time permits. Work and life keep one too busy, but the more I learn, the more interesing it gets. Not just commands or functions, but the math behind 'stuff'. That said, I could not imagine a better class than right here with friends.

    Well, I'd better get stuff caught up before work tonight. Hope a nice Sunday is being enjoyed by all,

    Mark

    @Paul: Thank you :-) I've used attributes, but did not catch that.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by GTO
    Well, I'm sure they do teach this in computer classes, but I could not attest one way or the other. Ya'll are my 'computer class' :-)

    I would like to take a class or two whenever time permits. Work and life keep one too busy, but the more I learn, the more interesing it gets. Not just commands or functions, but the math behind 'stuff'. That said, I could not imagine a better class than right here with friends.
    I know, I just couldn't resist

    I am thinking of taking an MSc next year, finances and entry allowing, in BI using the MS stack. I must admit it quite excites me and scares me in equal measures, I haven't done nthat sort of disciplined learning in many years. Can't get left behind by my daughters though.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,094
    Location
    Left behind...... Are we on the same planet???
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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