counting matching bits / bitXnor
Cees de Groot
cdegroot at gmail.com
Mon Dec 17 13:19:00 UTC 2007
On Dec 17, 2007 1:49 PM, Bert Freudenberg <bert at freudenbergs.de> wrote:
> > 0 bitXnor: 0 -> -1
>
> And what exactly is wrong with that?
>
err... I'd expect '1' here? The problem is that bitInvert is not
identical, as far as I can tell, with bitNot...
1 bitInvert -> -2
(where '1 bitNot' should result in 0)
And for the problem itself: drawing a quick list of truth tables of
available bit ops quickly pointed out that counting similar bits with
XNOR and counting different bits with XOR are equivalent ;-P. That's
what you get for firing off posts at the end of your lunch break.
Making the number of matching bits a similarity indicated based on the
length of the largest bit string, I currently have (pending more unit
tests ;-)):
findSimilarityBetween: n1 and: n2
"check how similar n1 and n2 are. Similarity is defined of the maximum
matching number of bits - if n1 << n2, we'll 'slide' n1 along n2'''"
| small large match thisMatch thisMatchBitcount nBits |
(n1 = n2) ifTrue: [^1.0].
(n1 < n2)
ifTrue: [small := n1. large := n2]
ifFalse: [small := n2. large := n1].
match := 0.
nBits := large highBitOfMagnitude.
1 to: nBits do: [:i |
thisMatch := small bitXor: large.
thisMatchBitcount := nBits - (self countBits: thisMatch).
(thisMatchBitcount > match)
ifTrue: [match := thisMatchBitcount].
small := small << 1].
^match / (nBits * 1.0)
--
"Human beings make life so interesting. Do you know, that in a
universe so full of wonders, they have managed to invent boredom. " -
Death, in "The Hogfather"
More information about the Squeak-dev
mailing list
|