Why does this IF AND statement work?

evanscamman

Registered User.
Local time
Today, 03:21
Joined
Feb 25, 2007
Messages
274
I'm trying to understand the logic behind this IF statement:

Code:
const WS_MAXIMIZE as long = &H1000000
lngStyle = GetWindowLong(frmhWnd, GWL_STYLE)
 
If lngStyle And WS_MAXIMIZE Then
    lngWndState = SW_SHOWMAXIMIZED
ElseIf lngStyle And WS_MINIMIZE Then
    lngWndState = SW_SHOWMINIMIZED
Else
    lngWndState = SW_SHOWNORMAL
End If

This code works, but i can't understand how.
My guess is that it is something to do with Constants and how they can be evaluated...

What does it mean: IF this AND That Then
If this what? if This and That equal something? If This and That are > than 0? If This is smaller than That?

Evan
 
Okay, here you go:

Sets a constand to a certain hex value
Code:
const WS_MAXIMIZE as long = &H1000000

Sets this long integer to a value based on the function GetWindowLong
Code:
lngStyle = GetWindowLong(frmhWnd, GWL_STYLE)

This:
Code:
If lngStyle And WS_MAXIMIZE Then
could be rewritten to this equivalent:
Code:
If lngStyle <> 0 And WS_MAXIMIZE <> 0 Then

I think the rest is pretty self-explanatory.

Does that help?
 
Here's what i don't understand:

? lngstyle
399441920

? WS_MAXIMIZE
16777216

? WS_MINIMIZE
536870912


Now, how does this work?

? lngStyle And WS_MAXIMIZE
16777216

? lngStyle And WS_MINIMIZE
0

Why does the 1st return a value and the 2nd return zero?
Is it somehow determining if WS_MAXIMIZE is a multiple of lngStyle - thus, it knows that the window is Maximized - among other things?

Evan
 
It is BOTH not just one:

test for

? lngStyle

and

? WS_MINIMIZE
 
I believe I did that - both lngStyle and WS_MINIMIZE have a value (look at my previous post). so why do they evaluate to zero?

Evan
 
Last edited:
What about this? Can you explain it?

A = 1000
B = 100

? A and B
96

???? What is going on here? I know it is important :)
Evan
 
AND is a binary "and" operation.

In other words, if you "and" 2 numbers together you will get a number that represents the bits that are set after the AND operation.

If you use an AND in a place where it can be evaluated to true or false, it will always evaluate non-zero numbers to true and zero numbers to false.

0 AND 1 evaluates to 0, false.
1 AND 1 evaluates to 1, true.
1000 base 10 AND 100 base 10 evaluates to 1111101000 base 2 AND 1100100 base 2 which evaluates to 1100000 base 2 which is 96 base 10, true.

It's all binary/boolean math. Use it to your advantage.

Edit: be careful. In an If statement and in some other places each individual statement is evaluated to true or false before the AND operation. "If 2 AND 4 then" would be true, since 2 is not zero and 4 is not zero. However, "2 AND 4" will evaluate to 0 in the immediate window, which is normally thought of as false.
 
Last edited:
To expand on George's very astute response, Visual Basic for whatever reasons has chose to not distinguish between logical and bitwise operators. If you look at the help menus for operators such as AND, OR, XOR, NOT, IMP, EVU, you will see two entries, one explaining for usual integers and other data types and another for bitwise (e.g. binary).

Here's a great article on explaining the bitwise operations. As you read the article, you will see that the main use with this AND operator is to check whether two bit flags are up, and do something based on it.
 
1000 base 10 AND 100 base 10 evaluates to 1111101000 base 2 AND 1100100 base 2 which evaluates to 1100000 base 2 which is 96 base 10, true.

Ok, now this is starting to mean something.
I get the 1st step - 1000 = 1111101000 and 100 = 1100100

But how do 1111101000 and 1100100 evaluate to 1100000?

Evan
 
Last edited:
As George said:
0 AND 1 evaluates to 0, false.
1 AND 1 evaluates to 1, true.

You then do addition like thus
Code:
  111110100
+ 001100100
===========
  001100100

Note that on the column where there are two 1s, the output column is 1 as well, otherwise it's 0. (I'm not sure why he said the output would be 110000, though)
 
It doesn't. But that wasn't the original problem. It was: 1111101000 AND 1100100.

Each "place" in a binary number is evaluated separately by the AND (not addition) operation (also: OR, NAND, NOR, XOR, NOT, etc.).

So, the first digit of each number is "0", thus the first digit of the "answer" will be "0". And so forth:

First Number AND Second Number = Answer
0 AND 0 = 0 (Ones)
0 AND 0 = 0 (Twos)
0 AND 1 = 0 (Fours)
1 AND 0 = 0 (Eights)
0 AND 0 = 0 (Sixteens)
1 AND 1 = 1 (Thirty-Twos)
1 AND 1 = 1 (Sixty-Fours)
1 AND 0 = 0 (One Hundred Twenty Eights)
1 AND 0 = 0 (Two Hundred Fifty Sixes)
1 AND 0 = 0 (Five Hundred Twelves)

Place it back on its side and you get: 1100000, or 96 base 10.

Does that help?
 
Last edited:
I'd like to point out that binary addition is exactly like decimal addition. So:

Assuming that "+" is a regular math operator (not boolean algebra):
Code:
   111110100 (aka 500)
+ 001100100 (aka 100)
===========
 1001011000 (aka 600)

Don't confuse the two.
 
I really shouldn't have had called it an addition, and you're right that in binary addition if I have two ones in one column, I put in zero and carry the one to left, just like the decimal addition when it "overflow".

However, the AND operator, as far as I understand it, isn't exactly binary addition; it only output ones whenever the column has two ones, zero for all other cases.

There's a trap, though. If we have Option Compare Database or Option Compare Text (is that right?) set, then I'd imagine that "A and a" would evaluate as true, even though the binary representation of A and a are quite different.
 
You are correct, AND is not binary addition. In fact, it is shown in boolean algebra with a multiplication operator ("*" or just putting the two operators next to each other like multiplication in regular algebra), though it is not multiplication either. In boolean algebra (the topic we're discussing when ANDing two numbers), the plus sign means "OR". So, in boolean algebra, you would read AB + CD (also A*B+C*D) as "(A and B) or (C and D)", which can be directly interpreted in VB (and SQL) or just "AB or CD", which means the same thing but cannot be interpreted by VB.

You got me on the Option Compare stuff. I just know the theory and have practiced using binary operations in VB since VB2. I honestly know very little (comparatively) about Access VBA in general and avoid answering questions about it unless they are pretty straight forward or potentially fun.

Oh crap, my toaster just attacked me! (lol...still love the xkcd stuff)
 
Ok, I'm beginning to understand. Correct me if this is wrong:
  • A 32bit binary number is like a row of 32 dip stitches. It's all very simple until you need to see the decimal equivalant of the 17th switch - then you get some bizarre number that doesn't seem to make any sense.
  • When the AND operator is used without a Comparison Operator, it performs a binary comparison of the two numbers - the 1st one is the row of switches, the 2nd number is the switch to check. Most of the time you would use a constant here. I'm guessing that all or most constants used this way would be products of 2 to the Nth power. (N = the switch number - from right to left).
So, now I want to put this to use:

I get the current state of my window with:
Code:
CurrentState =  getwindowlong (frmhwnd,gwl_style)
I have two constants:
Code:
Private Const WS_MAXIMIZE = &H1000000   '(or  16777216)
Private Const WS_MINIMIZE = &H20000000  '(or  536870912)
010111110011110000000000000000 (CurrentState)
000001000000000000000000000000 (WS_MAXIMIZE)
100000000000000000000000000000 (WS_MINIMIZE)

So, this statement will return 16777216:
Code:
If CurrentState And WS_MAXIMIZE Then ...

And this one will return 0:
Code:
If CurrentState And WS_MINIMIZE Then ...
 
Yep!

Note that when you compared WS_Minimze, you got 0 because there wasn't any column where there were two bits; it only takes one column to evaluate as true, even if it was the only column out of 32 columns.

BTW, placement isn't important.

a AND b = b AND A.
 

Users who are viewing this thread

Back
Top Bottom