Files
ruby/doc/string/bit_clear.rdoc
HASUMI Hitoshi 03a426ec8d [Feature #22279] Add region arguments to String bit operations (#18562)
This patch extends the following String methods:

* String#bit_set(offset, length, lsb_first: true) -> self
* String#bit_set(range, lsb_first: true) -> self
* String#bit_clear(offset, length, lsb_first: true) -> self
* String#bit_clear(range, lsb_first: true) -> self
* String#bit_flip(offset, length, lsb_first: true) -> self
* String#bit_flip(range, lsb_first: true) -> self
* String#bit_count(offset, length, lsb_first: true) -> Integer
* String#bit_count(range, lsb_first: true) -> Integer

Link: [Feature #22279]
2026-09-14 17:30:15 +09:00

35 lines
1.3 KiB
Plaintext

Sets the bit at zero-based bit +offset+ to 0; returns +self+:
s = "\xFF"
s.bit_clear(1) # => "\xFD"
s # => "\xFD"
With +length+, clears the +length+ consecutive bits beginning at +offset+;
with a Range, clears the bits covered by +range+:
s = "\xFF\xFF"
s.bit_clear(4, 8) # => "\x0F\xF0"
s = "\xFF\xFF"
s.bit_clear(4..11) # => "\x0F\xF0"
The entire region must lie within +self+; a region that extends beyond the
end raises +IndexError+ rather than being clamped. Writing zero bits is a
no-op, but even an empty region raises +IndexError+ when it begins beyond
the end of +self+.
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:
s = "\xFF"
s.bit_clear(1, lsb_first: false) # => "\xBF"
s = "\xFF\xFF"
s.bit_clear(4, 8, lsb_first: false) # => "\xF0\x0F"
Raises +IndexError+ if +offset+ is out of range, or if any part of the
region lies outside +self+.
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+.
Raises +FrozenError+ if +self+ is frozen, even when writing zero bits.