473,498 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bit enumeration algorithms

I have an algorithm question for you all:

I am familiar with table-free bit counting algorithms such as the MIT
HAKMEM algorithm and others. However, I have an application where I
need to enumerate the indexes of every bit in an integer (64 bit
integer in this case). For example, if I had the following 8-bit
integer:

01001000

I want an array to contain the numbers [ 3,6 ] along with an integer
'2' telling me that two bits were set.... or I want the same
information in some other form.

So right now what I'm doing is looping, testing, and shifting. It's
not bad, but I'm wondering if there's anything faster.

I tried inline assembly language using the 'bsf' instruction (bit scan
forward), but found that using this instruction seems to be *slower*
(yeah, I was surprised too!) than just the naive loop-test-shift even
though far fewer instructions were executed in the 'bsf'
implementation. I think this instruction uses a lot of clock cycles.
I know it's a rarely used instruction, so it's possible that modern
processors choose to implement it inefficiently to make room for more
efficient implementations of commonly used instructions. (This was an
Athlon-MP machine.)

So right now loop-test-shift with no special inline assembly language
voodoo is the fastest thing I've come up with. I do of course test for
the integer being zero as it loops, so it will abort the loop if no
more bits are left.

By the way, what I end up doing at the end of this is to pick a set bit
at random. If there's any fast shortcut to doing this and skipping the
whole bit enumeration, that would work too. Note that in my
application the bits are usually going to be sparse, so just testing
random bits turns out to be no better than enumerating bits and then
picking one.

So any wizards want to take this one up?

-Adam

Aug 3 '06 #1
3 2023
ad************@gmail.com wrote:
I am familiar with table-free bit counting algorithms such as the MIT
HAKMEM algorithm and others. However, I have an application where I
need to enumerate the indexes of every bit in an integer (64 bit
integer in this case).
Why not use a table? You can use a binary search to reduce the range
from 64 bits to something appropriate, like 16 or 8, and then a direct
table operation to get the last bits of the index, to either the MSB
or LSB as you like. Turn off that bit and repeat to get another index.
Need more speed? Don't need to repeat binary search until byte it found
is used up; then only need search of remaining bytes.
Aug 3 '06 #2
Robert Mabee wrote:
Why not use a table? You can use a binary search to reduce the range
from 64 bits to something appropriate, like 16 or 8, and then a direct
table operation to get the last bits of the index, to either the MSB
or LSB as you like. Turn off that bit and repeat to get another index.
Need more speed? Don't need to repeat binary search until byte it found
is used up; then only need search of remaining bytes.
That's a good idea that for some reason I hadn't thought of.

One way to do it would be to have two tables. The first table would be
a table containing all possible search orders to search the 8 bytes of
a 64-bit integer. The second table would contain bit enumerations for
all possible values from 0x0 to 0xff (hex).

Step 1: pick a search order at random from the first table

Step 2: iterate according to the search order through the bytes in the
64-bit integer until a nonzero byte is found

Step 3: look up it's bit enumeration from the second table

Step 4: pick a random set bit and return

That's pretty good. when I get a chance I'll implement it and see how
it does vs. the naive method.

Any other ideas out there?

-Adam

Aug 3 '06 #3
ad************@gmail.com wrote:
One way to do it would be to have two tables. The first table would be
a table containing all possible search orders to search the 8 bytes of
a 64-bit integer. The second table would contain bit enumerations for
all possible values from 0x0 to 0xff (hex).

Step 1: pick a search order at random from the first table

Step 2: iterate according to the search order through the bytes in the
64-bit integer until a nonzero byte is found

Step 3: look up it's bit enumeration from the second table

Step 4: pick a random set bit and return
If I understand what you're proposing, it will not give uniform weight
to the possible outcomes, which probably is required. Suppose one byte
has one set bit and another has 8. The two bytes have equal probability
of being chosen but one divides that probability among 8 outcomes.

How about a binary search using a random preference for which half of
the range to check? Something like
for (int pos = 0, step = 32; step >= 1; step >>= 1)
if (random_bits & step)
{ if (arg_bits >pos + step) pos += step;
}
else if (((arg_bits >pos) & (1 << step) - 1) == 0)
pos += step;
Aug 3 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
12612
by: Justin Wright | last post by:
I know that I can set up an enumeration as follows ( just typed in quick so may have syntax errors ): <xsd:simpleType name="colors"> <xsd:restriction base="xsd:string"> <xsd:enumeration...
8
6495
by: aevans1108 | last post by:
Greetings I can't seem to inherit enumerated values from a globally defined type in my XML schema. XmlSchema.Compile() doesn't like it. Here's the schema. <?xml version="1.0"...
2
1794
by: Mark | last post by:
Assume you have an enumeration like PhoneType { Home, Business, Cell }. This enumeration corresponds with a lookup/dictionary table in your database like: phone_cd | phone_descr 1 ...
4
5638
by: Marshal | last post by:
Sure... IEnumerable was inconvenient suggesting a separate class to service the enumeration, IEnumerator, and multiple operations: Current, MoveNext, Reset. (I'll warp the definition of "operation"...
1
4966
by: Stefano G. | last post by:
I have a WSDL containing this enumeration type <xsd:simpleType name="item_type_enum"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="VCD"/> <xsd:enumeration value="SVCD"/>...
27
2634
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
3
2520
by: Davidoff | last post by:
Hi, I parse an XML file with a XSD schema. One XmlNode has an attribute whose type is a restriction of xs:string : <xs:simpleType name="stypeDay"> <xs:restriction base="xs:string">...
0
500
by: news.emn.fr | last post by:
Hello, i got this attribute <xs:attribute name="jour"> <xs:simpleType> <xs:restriction base="stypeJour"> </xs:restriction> </xs:simpleType> </xs:attribute>
0
360
by: adam.ierymenko | last post by:
I have an algorithm question for you all: I am familiar with table-free bit counting algorithms such as the MIT HAKMEM algorithm and others. However, I have an application where I need to...
0
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6993
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7162
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6881
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.