Class: Integer
- Inherits:
-
Object
- Object
- Integer
- Defined in:
- lib/integer.rb
Instance Method Summary collapse
-
#hamming_distance_to(integer) ⇒ Object
Hamming distance – number of different bits in same positions H(1001, 1110) = 3 H(1001, 1000) = 1.
Instance Method Details
#hamming_distance_to(integer) ⇒ Object
Hamming distance – number of different bits in same positions H(1001, 1110) = 3 H(1001, 1000) = 1
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/integer.rb', line 5 def hamming_distance_to(integer) total = 0 difference = self ^ integer while difference > 0 do total += 1 if (difference & 1).nonzero? difference >>= 1 end total end |