Results 1 to 4 of 4
  1. #1
    pdanes is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Sep 2019
    Posts
    208

    Display bits in a flag variable

    I frequently use bits in numeric variables as flags. During testing and debugging, I sometimes need to know which bits are set or cleared. Debug.Print or mousing over the variable while execution is paused is fine if I'm only using a few bits, but when the number of flag bits gets large, decoding the displayed value is a monumental PIA.

    I've written a small function that shows the bits. No big deal, anyone can do that, but I've added a formatting option.



    The function takes two parameters – the flag variable and an optional mask string. If the mask is not supplied, the function spits back a string broken into bytes. But sometimes I have things like three bits for one related set of conditions, then two more for another set, then five, then one, and so on. In such a case, it is more helpful to me to see the breaks between the related groups, rather then arbitrarily on byte boundaries.

    The mask string looks for spaces. When there is a non-space, it outputs a one or zero for the bit value. When there is a space, it adds a space in the corresponding position of the output string. When there is a mask, it will generate a string at least as long as the mask, longer if necessary to show all set bits. When the mask is shorter than the generated string, it packs all the remaining bits together.

    When there is no mask, the function stops when it runs out of set bits.

    This is a programmer's debugging tool, so there is no provision for gracefully handling problems, but I see very little likelihood that displaying a bit pattern would ever be used in production anyway.
    Code:
    Public Function ShowBits$(ByVal hodnota&, Optional ByVal maska$)
    Dim i&, m&, l&, JeMaska As Boolean
    l = Len(maska)
    JeMaska = l > 0
    m = 1
    Do
        If CBool(hodnota And m) = True Then
            ShowBits = "1" & ShowBits
        Else
            ShowBits = "0" & ShowBits
        End If
        m = m * 2
        i = i + 1
        If (m > hodnota) And ((JeMaska = False) Or ((JeMaska = True) And (i >= l))) Then Exit Function
        If (l > 0) Then
            If (l > i) Then
                If Mid$(maska, l - i, 1) = " " Then
                    ShowBits = " " & ShowBits
                    i = i + 1
                End If
            End If
        Else
            If (i Mod 4) = 0 Then ShowBits = " " & ShowBits
        End If
    Loop
    End Function

  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
    You do not have a question, there is no issue? Just offering code sample? Maybe this should go in Code Repository forum.
    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
    pdanes is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Sep 2019
    Posts
    208
    Quote Originally Posted by June7 View Post
    You do not have a question, there is no issue? Just offering code sample? Maybe this should go in Code Repository forum.
    Yes, this is just a bit of code I thought might be handy for people. I do not know where the code repository forum is - when I come up with something I think might be useful, I generally post it in a forum that I frequent.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Click Forum button or Forum in the breadcrumbs line at top of thread or https://www.accessforums.net/index.php
    Scroll down to Access Knowledge Base section.

    Can report your original post and ask a moderator to move thread.
    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.

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

Similar Threads

  1. Command to display value of a variable
    By msmithtlh in forum Programming
    Replies: 2
    Last Post: 06-01-2022, 06:20 AM
  2. Replies: 2
    Last Post: 05-14-2021, 08:39 AM
  3. 4 bits of help please
    By quinnb in forum Access
    Replies: 7
    Last Post: 10-09-2017, 10:33 AM
  4. break a string into smaller bits
    By jmss96 in forum Queries
    Replies: 3
    Last Post: 12-18-2014, 11:57 AM
  5. Massive Data Bits Against Timeline of Events
    By Armorica in forum Database Design
    Replies: 1
    Last Post: 05-24-2013, 07:07 AM

Tags for this Thread

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