• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 141
  • Last Modified:

Retreiving the value of each bit of a Long value

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.


1 Solution
[ fanpages ]IT Services ConsultantCommented:


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:



Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now