Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method · added in Ruby 4.1

String#bit_count

bit_count → integer
bit_count(offset, length, lsb_first: true) → integer
bit_count(range, lsb_first: true) → integer

Returns the number of set bits in self.

With no arguments, counts over all bytes of self:

Example
"\x00".bit_count # => 0
"\xFF".bit_count # => 8
"\xAA".bit_count # => 4

With offset and length, counts only the length bits beginning at zero-based bit offset; with a Range, counts the bits covered by range:

Example
data = "\xFF\x00\xF0"
data.bit_count(0, 8)   # => 8
data.bit_count(8, 8)   # => 0
data.bit_count(0..7)   # => 8
data.bit_count(8...16) # => 0
data.bit_count(16..)   # => 4

A region that extends beyond the end of self is clamped to the bits that exist; a region that begins at or beyond the end counts zero:

Example
data.bit_count(16, 100) # => 4
data.bit_count(100, 8)  # => 0

By default, bits within each byte are numbered from least-significant to most-significant. If lsb_first is false, byte order is unchanged but bits within each byte are numbered from most-significant to least-significant. The numbering matters only when the region is not byte-aligned; the no-argument form is independent of it:

Example
"\xF0".bit_count(0, 4)                   # => 0
"\xF0".bit_count(0, 4, lsb_first: false) # => 4

The count is over the bytes of self and is independent of string encoding. Raises IndexError if offset or a Range endpoint is negative. Raises ArgumentError if length is negative. Raises ArgumentError if a bit position is too large to be represented. Raises ArgumentError if lsb_first is neither true nor false.

Ruby version changes

  1. 4.1