From: "jhawthorn (John Hawthorn) via ruby-core" Date: 2026-07-07T02:18:08+00:00 Subject: [ruby-core:125943] [Ruby Feature#20163] Introduce #bit_count method on Integer Issue #20163 has been updated by jhawthorn (John Hawthorn). I tried finding uses in the wild via gem-codesearch. I only looked for `to_s(2).count("1")` which is a pretty inefficient way to `bit_count`, so this would miss any gems which used another technique (a loop or some bit twiddling) to count bits. Still it found a few use cases: **CIDR/netmask** Very common to do some variation of `IPAddr.new(mask).to_i.to_s(2).count("1")` to get the prefix length of the netmask. Done by: sequel, facter, inspec-core, vagrant, and many others Another case (in stdlib!) is `IPAddr#prefix` which uses a shift loop. If `bit_count` is accepted we should change [the loop in `IPAddr#prefix` to use it](https://github.com/ruby/ipaddr/blob/master/lib/ipaddr.rb#L464-L480). **Hamming distance** For example [dhash-vips.rb](https://github.com/nakilon/dhash-vips/blob/master/lib/dhash-vips.rb) doing `to_s(2).count("1")` to get hamming distance for perceptual similarity Also: simhash2, chromaprint, edits, dhash, dhasher, and others Also found in [unicode_plot](https://github.com/red-data-tools/unicode_plot.rb/blob/4c313e47449d894d055ccfb51df38487cdb2a039/lib/unicode_plot/canvas/ascii_canvas.rb#L102) using a different pattern: `(i ^ k).digits(2).sum` **Parity bit** For example in https://github.com/larskanis/pkcs11 `n.to_s(2).count("1").odd?` **sigstore-ruby** I only found sigstore Ruby doing this, something involving proof verification in a Merkle tree (?). It requires both bit_length (which already exists) and bit_count (being proposed here). `(log_index >> inner).to_s(2).count("1")` https://github.com/sigstore/sigstore-ruby/blob/26ffbe0c1d6e16614fb9cd8b2ed40ab47d57d421/lib/sigstore/internal/merkle.rb#L82-L87 Note: I believe this was ported from go which has a popcount (`bits.OnesCount64`) method. ---------------------------------------- Feature #20163: Introduce #bit_count method on Integer https://bugs.ruby-lang.org/issues/20163#change-117911 * Author: garrison (Garrison Jensen) * Status: Open ---------------------------------------- This feature request is to implement a method called #bit_count on Integer that returns the number of ones in the binary representation of the absolute value of the integer. ``` n = 19 n.bit_count #=> 3 (-n).bit_count #=> 3 ``` This is often useful when you use an integer as a bitmask and want to count how many bits are set. This would be equivalent to ``` n.to_s(2).count("1") ``` However, this can be outperformed by ``` def bit_count(n) count = 0 while n > 0 n &= n - 1 # Flip the least significant 1 bit to 0 count += 1 end count end ``` I think this would be a useful addition because it would fit alongside the other bit-related methods defined on integer: `#bit_length,` `#allbits?`, `#anybits?`, `#nobits?`. Also, when working with bitmasks, a minor upgrade to performance often results in a significant improvement. Similar methods from other languages: https://docs.python.org/3/library/stdtypes.html#int.bit_count https://doc.rust-lang.org/std/primitive.i32.html#method.count_ones -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/