Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hey there

How do you get the value of a bitarray.

Not the individual items but of the whole array?

After setting the individual bits I want the value of the the array

Dim baParameter As New BitArray(7)

baParameter.Set(3,True)
baParameter.Set(5,True)
baParameter.Set(2,True)
baParameter.Set(1,True)

Dim i as Integer=baParameter.value ' Don't work!

At the end of this snippet I want to have i = 46 (2^5+2^3+2^2+2^2)

 

Regards

/Kejpa

Posted

.value??? no such animal

but all problems except you know what have a solution

 

Public Function SumBits(ByVal ba As BitArray) As Integer
   Dim accum As Double = 0.0
   Dim i As Integer
   For i = 0 To ba.Count - 1
       If ba(i) Then
           accum += 2 ^ i
       End If
   Next
   Return CInt(accum)
End Function

IN PARVUM MULTUM
Posted

:)

I know there's no such animal but what I can't figure out is why Noah never brought it onboard.

I've solved it the same way you suggested but I thought there were a handier way.

 

/Kejpa

  • *Experts*
Posted

If your endgoal is to get this as an int, I'd suggest using BitVector32 (part of System.Collections.Specialized). It's faster and easier to use, mostly.

 

I don't know if there's a handier way of converting a BitArray into an int than the manual looping approach you mention. You might be able to use CopyTo to get the bits into a different array and then convert that array into an int, but that seems like almost as much work.

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

BitVector32 simple?!?

 

If your endgoal is to get this as an int' date=' I'd suggest using BitVector32 [/quote']

Maybe not endgoal, but something to use along the way, yes.

 

It's faster and easier to use' date=' mostly.[/quote']

Faster, so they say. Easier, I've heard that too, didn't believe it then either, later proved me right ;)

 

Challange: Set 8 bits and return value. This is the way I would like it done, but it's not working, returns 7 instead of 255

Dim bv As System.Collections.Specialized.BitVector32
bv.Item(0) = 1
bv.Item(1) = 1
bv.Item(2) = 1
bv.Item(3) = 1
bv.Item(4) = 1
bv.Item(5) = 1
bv.Item(6) = 1
bv.Item(7) = 1
MsgBox(bv.Data)

 

Regards

/Kejpa

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...