Re: Hamming Distance

2008-06-27 Thread Jared Grubb
Matimus, I was surprised that "lazy" was the algorithm that won your time tests, and I saw a way to improve it even better (algorithm is O(# ones in number) rather than O(# bits in number)) def lazy2(a, b, bits=32): x = (a ^ b) & ((1 << bits) - 1) tot = 0 while x: tot += 1

Re: Hamming Distance

2008-06-27 Thread Hans Terlouw
godavemon wrote: I need to calculate the Hamming Distance of two integers. The hamming distance is the number of bits in two integers that don't match. ... What about letting the bits count themselves in a parallel adding scheme: def hamming(i, j): i ^= j i = ((i&044)>>2)+((i&02

Re: Hamming Distance

2008-06-19 Thread godavemon
Great thanks! On Jun 19, 5:37 pm, Matimus <[EMAIL PROTECTED]> wrote: > On Jun 19, 4:27 pm, godavemon <[EMAIL PROTECTED]> wrote: > > > > > I need to calculate the Hamming Distance of two integers. The hamming > > distance is the number of bits in two integers that don't match. I > > thought there

Re: Hamming Distance

2008-06-19 Thread godavemon
Awesome! Thanks a lot. On Jun 19, 5:00 pm, Mensanator <[EMAIL PROTECTED]> wrote: > On Jun 19, 6:27 pm, godavemon <[EMAIL PROTECTED]> wrote: > > > > > I need to calculate the Hamming Distance of two integers. The hamming > > distance is the number of bits in two integers that don't match. I > >

Re: Hamming Distance

2008-06-19 Thread Raymond Hettinger
Non-recursive, 8-bit block table lookup version: def ham(a, b, ht=[hamdist(a,0) for a in range(256)]): x = a ^ b dist = 0 while x: dist += ht[x & 255] x >>= 8 return dist Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Hamming Distance

2008-06-19 Thread Matimus
On Jun 19, 4:27 pm, godavemon <[EMAIL PROTECTED]> wrote: > I need to calculate the Hamming Distance of two integers.  The hamming > distance is the number of bits in two integers that don't match.  I > thought there'd be a function in math or scipy but i haven't been able > to find one.  This is my

Re: Hamming Distance

2008-06-19 Thread Mensanator
On Jun 19, 6:27 pm, godavemon <[EMAIL PROTECTED]> wrote: > I need to calculate the Hamming Distance of two integers.  The hamming > distance is the number of bits in two integers that don't match.  I > thought there'd be a function in math or scipy but i haven't been able > to find one.  This is my

Re: Hamming Distance

2008-06-19 Thread Robert Kern
godavemon wrote: I need to calculate the Hamming Distance of two integers. The hamming distance is the number of bits in two integers that don't match. I thought there'd be a function in math or scipy but i haven't been able to find one. This is my function but it seems like there should be a

Re: Hamming Distance

2008-06-19 Thread John Machin
On Jun 20, 9:27 am, godavemon <[EMAIL PROTECTED]> wrote: > I need to calculate the Hamming Distance of two integers. The hamming > distance is the number of bits in two integers that don't match. I > thought there'd be a function in math or scipy but i haven't been able > to find one. This is my

Re: Hamming Distance

2008-06-19 Thread Raymond Hettinger
On Jun 19, 4:27 pm, godavemon <[EMAIL PROTECTED]> wrote: > I need to calculate the Hamming Distance of two integers.  The hamming > distance is the number of bits in two integers that don't match.  I > thought there'd be a function in math or scipy but i haven't been able > to find one.  This is my