Issue #20163 has been updated by jhawthorn (John Hawthorn). Couple notes on the various gems mame (Yusuke Endoh) wrote in #note-31:
* **Parity bit**: I couldn't find the relevant code in larskanis/pkcs11.
Sorry, the quoted code was from another library. Here it is: https://github.com/larskanis/pkcs11/blob/12d868912e0d17aa41e497f9bb99f66a397... Another example of parity: https://gitlab.com/pkerling/subconv/-/blob/e560905808b2db0918b0e350f39b13690...
* **slonopotamus/dyck**: This uses it in the form `Dyck.popcount(tagx.bitmask) > 1`, but unless I'm misreading, `tagx.bitmask` seems to be a value like `0b1`, `0b11`, `0b111`, ..., so `bit_length > 1` seems more natural than `popcount > 1`.
These aren't equivalent. This is checking that MORE than one bit is set, I don't know why but [other mobi libraries in other languages](https://github.com/bfabiszewski/libmobi/blob/906274205c11944b628da1c553b255a...) seem to as well. But I agree there's another way to test for this: `x & (x - 1) != 0`
* **nerzh/ton-sdk-ruby**: This one is genuinely popcount. Worth noting that this library also defines `clz`.
It defines `clz` (for 32 bits by default), but only seems to use it in `level` as `32 - clz(value)`, which is actually `bit_length`. So this is another case `bit_count` is complementing `bit_length`. ---------------------------------------- Feature #20163: Introduce #bit_count method on Integer https://bugs.ruby-lang.org/issues/20163#change-117953 * 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/