mark007 Posted July 1, 2005 Posted July 1, 2005 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. :) Quote Please check the Knowledge Base before you post. "Computers are useless. They can only give you answers." - Pablo Picasso The Code Net
Leaders Iceplug Posted July 1, 2005 Leaders Posted July 1, 2005 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 :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
mark007 Posted July 1, 2005 Author Posted July 1, 2005 Thanks, Iceplug. I'd not seen the bitshift operator before. Very useful! :) Quote Please check the Knowledge Base before you post. "Computers are useless. They can only give you answers." - Pablo Picasso The Code Net
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.