Results 1 to 8 of 8

Thread: Confused about using NOT and AND operators

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Confused about using NOT and AND operators

    I have two If statements using the NOT operator:

    1) If Not 3 And &H88 Then

    2) If Not 15 And &H88 Then

    In both cases control goes into the If clause.

    What confuses me is the result of (1) is 0 (3 And &H88 = 0) which to me would cause control to enter the If clause but the results of (2) is 8 (15 And &H88 = 8) which to me should skip the If clause and exit out.

    I'm trying to mimic some C code and the C code is this:

    Code:
     if(!((isqTo = isqFrom + *pvec) & 0x88))
     {
        '
        '
     }
    Now when the above is executed the first time isqFrom = 4, *pvec points to memory that contains the value -1 and the results in isqTo is 3 and after the & 0x88 control enters the if clause just like it does in the VB example. Now the second time the if is executed isqFrom = 1, *pvec = 14 and isqTo = 15 and after the & 0x88 control skips out of the if statement.

    So, in the VB I have this:

    Code:
      '
      '
     isqTo = isqFrom + pvec(i)
    
     If isqTo > 0 Then
       '
       ' 1st time isqTo is 3
       ' 2nd time isqTo is 15 
       If Not isqTo And &H88 Then
           '
           ' 
           '
       End If
     End If
        '
        '
    The variables in the VB code are the same as they are in the C code


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Confused about using NOT and AND operators

    So what is the confusion?

    Code:
    Debug.Print 3 And &H88
    Debug.Print 15 And &H88
    Yields

    0
    8

    Not 0 would be true
    Not 8 would be false

    As 0 is false and anything other than 0 is true the Not gives the opposite result

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Confused about using NOT and AND operators

    After a couple of tests I think I see where the confusion is.

    Perhaps what you wanted was
    Code:
    If Not CBool(isqTo And &H88) Then
           '
           ' 
           '
       End If
    I really would have thought the code you had would have did this but after testing it I did not see the result I expected.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Confused about using NOT and AND operators

    Since all the values are numeric, the code is doing a binary Not, so is flipping all the bits, so the result of (15 And &H88) is non-zero and the inverse of it is still non-zero.
    Changing the non-zero value to a boolean first, then does a logical Not, so non-zero inverted become logically a 0, i.e false.

    Its another example of C being clear with two operators, and VB being context sensitive because it uses the same operator for two different operators.
    In C you boolean logical Not "!", and binary operator Not "~".
    In VB you use "And" for both, and the context of the variables operated on determine whether it is logical Not or binary operation Not.
    Last edited by passel; Dec 22nd, 2014 at 06:14 PM.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Confused about using NOT and AND operators

    Think links like this wikipedia one might be helpful
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: Confused about using NOT and AND operators

    FYI, I just made a mistake by using Not Len(text):

    Code:
    Dim text As String
       text = "A"
       If Not Len(text) Then
          Debug.Print "Empty"
       Else
          Debug.Print "Has Text"
       End If
    I 'expect' "Not Len(text)" is equal with "If Len(text)=0 then", but VB Slapping me.

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Confused about using NOT and AND operators

    Quote Originally Posted by DataMiser View Post
    After a couple of tests I think I see where the confusion is.

    Perhaps what you wanted was
    Code:
    If Not CBool(isqTo And &H88) Then
           '
           ' 
           '
       End If
    I really would have thought the code you had would have did this but after testing it I did not see the result I expected.
    Wouldn't the not only be applied to "isqTo" in that case?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Confused about using NOT and AND operators

    Are you talking about "Not CBool(isqTo And &H88)" ?
    Both numbers are in parentheses so will be operated on first.
    isqTo is a number, and &H88 is a number, so those will be Binary Anded together, and the result converted to a boolean (True or False) by the CBool function, and that boolean logically negated to (False or True) by the Not operator.
    The two examples given,
    (3 And &H88) result is 0, so 0 is converted to False (0), then the Not returns True (-1).
    (15 And &H88) the result is 8. so 8 is converted to True (-1) and the Not returns False (0).

    Without the CBool
    (15 And &H88) gives 8, and the Not complements (ones) the bits, so becomes -9, and any non-zero value will be evaluated as "True" in an If statement, so both (3 and &H88) and (15 and &H88) would be evaluated as True by the If without the CBool.

    If you're talking the original OP example,
    If Not isqTo And &H88 Then

    Then, yes the Not inverts isqTo first, and Ands it with &H88, giving a different set of numbers, but still a non-zero result in a lot of cases that will be evaluated as True by the If, such is the case of both examples of 3 and 15.
    Of course, 3 is being inverted to -4, and Anded with &H88 resulting in 136, which is evaluated as True because it is non-zero, not because it was 0 (false) and logically inverted to True, so the assumption that 3 and &H88 was "working" wasn't really true.
    Last edited by passel; Dec 23rd, 2014 at 10:27 AM.

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