Re: scoring differences between bitmasks

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Ben <bench(at)silentmedia(dot)com>
Cc: Postgresql-General mailing list <pgsql-general(at)postgresql(dot)org>
Subject: Re: scoring differences between bitmasks
Date: 2005-10-02 12:39:51
Message-ID: 20051002123951.GC30492@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, Oct 01, 2005 at 07:12:13PM -0700, Ben wrote:
> I'm looking for a quick way to find the number of bits that are
> different between two bitmasks of the same size. I'll be doing this a
> lot, so speed is desirable.... some kind of indexing would be even
> better. It seems like this problem must have been solved before....

Step 1: Use XOR to get the bits that are different.
Step 2: Count the bits.

Something like x & ((~x) +1) will give you the value of the last
bit that is set, mask it out and repeat. If you need to do it a lot,
build a table of 256 values and then process 8 bits at a time. Should
be fairly straight forward...

Hope this helps,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2005-10-02 12:43:49 Re: varchar to text
Previous Message Zlatko Matić 2005-10-02 11:35:56 Re: installing several PostgreSQL instances on Windows