Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 139
  • 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.

Thanks
Bob



0
rkneal
Asked:
rkneal
1 Solution
 
[ fanpages ]IT Services ConsultantCommented:
Hi,

Try:

If j < 31 Then
    k = longValue And (2 ^ j))
Else
    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:

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_10122746.html

BFN,

fp.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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