Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

If I have a long value what is the fastest way to create an array of bits representing it?

 

For example if I have 1 I'll get:

 

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1

 

2 will give:

 

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0

 

3 will give:

 

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1

 

11 will give:

 

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1

 

etc.

 

By way of background I then want to sum this array and determine the max value if each 0 represnts an downward movement and each 1 represents an upward movement.

 

Therefore if anyone can think of a better way of generating the 2^64 possible arrays that would also be useful. Speed is very important.

 

Thanks.

 

:)

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

  • Leaders
Posted

You can loop through the bits.

 

For I = 0 To 63
 If Number And Convert.ToInt64(2 ^ I) = 0 Then
   Bit(I) = True
 Else
   Bit(I) = False
 End If
Next

Also, note that an If statement that returns True in the If block and False in the Else can be simplified to a single assignment:

For I = 0 To 63
 Bit(I) = Number And Convert.ToInt64(2 ^ I) = 0
Next

And if you are using .NET 2003 or later (can't tell from your profile), you can use bit shifts to eliminate the exponentiation.

For I = 0 To 63
 Bit(I) = Number And Convert.ToInt64(1 << I) = 0
Next

:)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

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...