Recently ran into the following syntax, which is unfamiliar to me:
If (X And 1) <> 0 then
Code
End If
X is an integer.
Can someone explain what the parenthetic "And" does?
Thank you.
Recently ran into the following syntax, which is unfamiliar to me:
If (X And 1) <> 0 then
Code
End If
X is an integer.
Can someone explain what the parenthetic "And" does?
Thank you.
Last edited by avr5iron; 09-01-2016 at 02:06 PM.
This looks like an excel function, not a VBA.
It is part of a FuzzyLookup function I found searching online. It is VBA, and it works perfectly. I just don't understand the parenthetic "And", within the if statement. The integer, "X", is an optional function argument (default = 3). Used to determine whether or not to execute a particular Sub, like...
If (X And 1) <> 0 Then
Alg1 Arg1, Arg2, Arg3
End If
In short, it checks whether the number is odd or even :
![]()
Please Login or Register to view this content.
1. I care dog
2. I am a loop maniac
3. Forum rules link : Click here
3.33. Don't forget to mark the thread as solved, this is important
Wow. Wouldn't have guessed that. Thank you.
You are welcome, thanks for marking the thread as solved.
If you want to dig a little deeper, here is the explanation :
For "AND" binary comparison :![]()
Please Login or Register to view this content.
0 and 0 = 0
0 and 1 = 0
1 and 0 = 0
1 and 1 = 1
Only both value is 1 will yield 1
Now check for 1 and 1 :
1 and 1 = 1
00000001
00000001
--------
00000001
Comparing first digit (the leftmost) :
0 and 0 --> 0
Comparing second digit (second from left) :
0 and 0 --> 0
...
Comparing last digit (first from right) :
1 and 1 --> 1
so the result is 00000001 in binary, which is number 1
Now check for 2 and 1 :
2 and 1 = 0
00000010
00000001
---------
00000000
Comparing first digit (the leftmost) :
0 and 0 --> 0
Comparing second digit (second from left) :
0 and 0 --> 0
Comparing third digit (third from left) :
0 and 0 --> 0
Comparing fourth digit (fourth from left) :
0 and 0 --> 0
Comparing fifth digit (fifth from left) :
0 and 0 --> 0
Comparing sixth digit (sixth from left) :
0 and 0 --> 0
Comparing seventh digit (seventh from left) :
1 and 0 --> 0
Comparing last digit (eightht from left) :
0 and 1 --> 0
so the result is 00000000 in binary, which is number 0
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks