Retreiving the value of each bit of a Long value

Posted on 2005-05-15
Last Modified: 2010-05-01
I am trying to interogate each bit of a Long value (32 bits). I use the following code to do so.

dim longValue as long
dim j as Integer
dim k as long

 For j = 0 To 31
  k = longValue And (2 ^ j))
  k = IIf(k > 0, "1", "0")
  rStr = rStr & k
 Next j

 msgbox rStr

When i get to 2^31 i get an overflow because this value is greater than that of a long.
Can anyone think of a beter way of finding the bit value of each bit in a long word. I am thinking of doing bitwise shifts to shift right the 32nd bit to avoid this.


Question by:rkneal
    1 Comment
    LVL 35

    Accepted Solution



    If j < 31 Then
        k = longValue And (2 ^ j))
        k = &H80000000 Or longValue
    End If

    instead of your existing code:

    k = longValue And (2 ^ j))

    Further routines in a previous thread here at E-E:



    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now