Results 1 to 14 of 14
  1. #1
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    What does this do?

    I'm working on some binary conversions, and ran across this expression:



    Code:
    (15 And &H1)
    Can you tell me what it does and why it evaluates to a 1?
    How about what MS documentation applies to this?

    Thanks

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    In VBA &H is used to define a color code. Review https://www.accessforums.net/showthread.php?t=88998

    Alone, &H1 evaluates to 1 in the immediate window.

    Out of context, no way to know what (15 And &H1) is supposed to do. Certainly isn't doing arithmetic or concatenation. And is a logical operator.

    Where did you find this?
    Last edited by June7; 11-26-2023 at 01:50 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    A binary converter

    Quote Originally Posted by June7 View Post
    In
    Where did you find this?
    Here is the code it was in. It seems to work, but it's in the category of "it works, but I don't know why":


    Code:
    Public Function DispBinary(ByVal lngVar As Long) As String
        Do While lngVar > 0
            DispBinary = Format(lngVar And &H01, "0") & DispBinary
            lngVar = Int(lngVar / 2)
        Loop
    End Function
    I messed it up when I copied the code and changed &H01 to &H1, but it still worked.
    Whatever it does, it seems quite elegant.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Little obvious really.
    It is converting a number to a binary string?

    &H1 is hexadecimal 1
    &H01 would still be 1
    &H00000001 is still 1

    ? dispbinary(255)
    11111111

    However Access removes those leading zeroes

    I agree though, that it is elegant and now curious as to why you would need it?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Why so obvious?

    Quote Originally Posted by Welshgasman View Post
    Little obvious really.
    It is converting a number to a binary string?
    Well yeah, I got that part, the OP was what this bit (pun intended) is doing?

    Code:
    lngVar and &H01

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    @Welshgasman post #6

    I'm not sure how that applies, it seems to calculate if the bit is 1 (or 0) for the highest position in the decimal number, which in reality is represented internally to VBA as a binary number, and I'm guessing the format rips off the "-" for a true condition. Wouldn't MS have been kind to just make a CBin() function?

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    It is ANDING 1 with each bit.
    If the bit in the value passed is 1, then you get a 1 as both are 1s, if not 0.
    Then you move on to the next bit, by dividing by 2, and so on, until you reach the end of the bits possible.

    Yes, a ready made function would be nice (I was wondering how long it was going to take for you to have a moan about MS and Access), so this is really your CBin() function. Easier to type as well?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    @ Welshgasman post #8.

    I think I stated the idea in post #7. The question, why is And working with just a bit, and why the highest order bit first. The hex value isn't just one bit nor is the passed in value.

    I know, let's make a bunch of variable types for numbers, and then some conversion functions. We won't do hex or binary, because hey, who would ever want to use those on a computer? The users can write a function in VBA so it can execute nice and slow with p-code instead of assembled code. Where is the MS Access documentation to explain this ANDING?

    Yes, I know I "whine", but know that it is in direct proportion to the time I spend working around dumb designs, missing or poor documentation, bugs, bugs and more bugs in a product that hasn't progressed much in the last 10 years in either enhancement, Microsoft 365 feel (are you still looking for that zoom feature or VSS compatibility?) or Internet/Wi-Fi capability? Some of the MVP's sites I read are a lot more "whiny" than me when it comes to their experience with MS supporting Access. This isn't anything new on my part, I plow through a lot of old Allen Browne blogs, and most of them start with a WTF Access?!!!

    I did read this,
    And operator | Microsoft Learn
    but I don't understand what they are trying to say about the bitwise comparison. A little better explanation or example would help.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    A 1 in place of &H1 provides same output.

    Exactly what does "ANDING" mean? I have never seen used this way (without an If or IIf). I gather that each side of AND is evaluating to 0 or 1 (nothing to do with True or False). Some experimenting shows that 0 and even numbers return 0 and odd numbers return 1. This expression in place of the Format() also provides same output: IIf(lngVar Mod 2 = 0, 0, 1). And my head stops spinning around the AND mystery.

    I have a function that converts decimal to binary but it is longer procedure. I like this new short and sweet version although the other has an additional argument which might make the longer code necessary.

    None handle 0 input. Wouldn't you want to return 0?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    Granted there is one MVP who is very negative about MS and Access.
    He is no longer posting new content but his negative rants do unfortunately have an impact due to his current status.
    His opinions are very different to those of other MVPs and to others posting Access related content on a regular basis.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by June7 View Post
    A 1 in place of &H1 provides same output.

    Exactly what does "ANDING" mean? I have never seen used this way (without an If or IIf). I gather that each side of AND is evaluating to 0 or 1 (nothing to do with True or False). Some experimenting shows that 0 and even numbers return 0 and odd numbers return 1. This expression in place of the Format() also provides same output: IIf(lngVar Mod 2 = 0, 0, 1). And my head stops spinning around the AND mystery.

    I have a function that converts decimal to binary but it is longer procedure. I like this new short and sweet version although the other has an additional argument which might make the longer code necessary.

    None handle 0 input. Wouldn't you want to return 0?
    I have never been that comfortable with binary logic TBF, but

    ? cbin(0)


    returns nothing, so either check for len of string or it's value?
    Using twgonder's complaint, I renamed it to CBin() :-)

    ? len(cbin(0))
    0

    Code:
    Public Function CBin(ByVal lngVar As Long) As String
        Do While lngVar > 0
            CBin = Format(lngVar And &H1, "0") & CBin
            lngVar = Int(lngVar / 2)
        Loop
    End Function
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    @IslaDogs post #11: I've read from more than one MVP (around six, most being developers for many years and having been bit several times by MS announcing something new and then in a few years saying "never mind", but I don't think this is the place to name names) that is a bit negative with MS involvement.

    To be honest, I have no idea what goes on at MS, just that everything Windows just seems to get more messed up in their effort to emulate Apple. Take Windows 11 Settings vs. the Control Panel. Now how can I keep the computer up at 100% during the incessant updates, without changing all the one-up sleep settings? Windows Explorer...Aggghhhh!

    The optimist: The glass is half full
    The pessimist: The glass is half empty
    The engineer: The glass is too big
    Me: Let's find some cleaner water and fill the glass up, please.

  14. #14
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    How my functions respond for setting permission flags:

    ?fConvNumber("1010101010101010101010101010100")
    1431655764
    ?fConvBinary(1431655764)
    1010101010101010101010101010100
    ?fConvBinary(1431655764,1)
    True
    ?fConvBinary(1431655764,31)
    False
    ?fConvBinary(0)
    0000000000000000000000000000000
    ?len(fConvBinary(0))
    31
    ?fConvNumber("1111111111111111111111111111111")
    2147483647

Please reply to this thread with any new information or opinions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums