PDA

View Full Version : Operators in Arguments question



GTO
06-04-2010, 07:09 PM
When using logical operators, such as Or (or) And in a common manner, such as:

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

...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:

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

or...

lngRet = InsertMenu(hMenuPopMisc, 1&, MF_POPUP Or MF_BYPOSITION Or MF_ENABLED, hMenuPop, "Viewable Months")

...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:

lWhatever = lSomeVariable Or A_Constant


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

p45cal
06-05-2010, 02:54 AM
I'm not sure, but don't they just resolve to TRUE or FALSE being passed as an argument?

Bob Phillips
06-05-2010, 02:58 AM
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.

GTO
06-05-2010, 08:55 PM
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.


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:
MsgBox "Hello World", vbDefaultButton2 Or vbExclamation Or vbOKCancel, ""

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 x = vbDefaultButton2 Or vbExclamation Or vbOKCancel 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/Courses/152/97F/Readings/student-binary.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:


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


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:


Decimal Binary
256 = 100000000
48 = 110000
1 = 1
----------
OR results in 100110001

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?

mdmackillop
06-06-2010, 02:58 AM
And BTW, besides being a nice donkey in Winnie the Poo, what is EOR?
Too good an opportunity!

Bob Phillips
06-06-2010, 02:59 AM
There is certainly so much that I have to learn. Sigh...

... and so little time



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 :(


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:
MsgBox "Hello World", vbDefaultButton2 Or vbExclamation Or vbOKCancel, ""

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.


So...the blonde guy, still thinking in decimal numbers, confirms by trying x = vbDefaultButton2 Or vbExclamation Or vbOKCancel 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 :devil2:


Okay, I read here:
http://www.math.grin.edu/%7Erebelsky/Courses/152/97F/Readings/student-binary.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).


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:


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


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:


Decimal Binary
256 = 100000000
48 = 110000
1 = 1
----------
OR results in 100110001

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.


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.

GTO
06-06-2010, 03:56 AM
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!:bow:

mdmackillop
06-06-2010, 04:10 AM
Glad you like it!

Paul_Hossler
06-06-2010, 06:53 AM
Another place that bit-wise operators and mappings frequent arise is with file attributes: From Help (Attributes)


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


Paul

GTO
06-06-2010, 04:51 PM
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!


... 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?!


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.


Wow Mark, you have realised that binary numbers are important to computers (never mind code) ... you are quick :devil2:

Hey, no double-teaming outta you and Malcom! Yes though, I can be awfully thick somedays:dunno


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.


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


ACK!, will have to study that.


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.


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


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.

Bob Phillips
06-07-2010, 01:46 AM
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.

Aussiebear
06-07-2010, 02:15 AM
Left behind...... Are we on the same planet???