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
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
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
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
> >
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
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
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
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
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
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
10 matches
Mail list logo