From: "jhawthorn (John Hawthorn) via ruby-core" Date: 2026-07-06T23:56:35+00:00 Subject: [ruby-core:125940] [Ruby Feature#20163] Introduce #bit_count method on Integer Issue #20163 has been updated by jhawthorn (John Hawthorn). I have a real-world use case: I've been working on a pure-Ruby [Hash Array Mapped Trie (HAMT)](https://en.wikipedia.org/wiki/Hash_array_mapped_trie) https://github.com/jhawthorn/hamt. I want this because having an immutable hash-table-like map which can be cheaply copied/modified would be helpful for sharing data between Ractors. The implementation of HAMT requires popcount: for each node in the tree we have a 32-bit bitmap representing which keys are present, and an array containing the keys/values. At each level we test for a key's presence using 5 bits of it's hash value `bit = 1 << ((hash >> level) & 31)`. If that bit is set, we find where it exists in the array using `popcount(bitmap & (bit - 1))` (feels similar to succinct bit vector). The `String#bit_count` proposed in another issues (#22082/#22118) would not be a good fit here. I don't want an additional String object per-node. I'm always representing at most 32 bits (this is the typical choice for HAMT) in a fixnum. I'd also need either a way to mask only the relevant bits from the string, requiring an extra allocation (I actually don't even see a way to do this with what is in #22118). We also have the endianness issue. This is all avoided and natural with Integer. There are other textbook uses for popcount out there: hamming distance, parity, etc. I think `Integer#bit_count` makes sense and is aligned with existing use of Integer. We have `bit_length` which is a very similar concept and name and have many other functions for dealing with bits on integer (`Integer#[]`, `#allbits?`, `#anybits?`, bitwise operations, etc) ---------------------------------------- Feature #20163: Introduce #bit_count method on Integer https://bugs.ruby-lang.org/issues/20163#change-117908 * 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/